Can't understand what i did wrong

problem code:COLGLF4
i can’t understand what i am doing wrong in this code.i have tried to match the editorial with code four times

#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	    int n,e,h,omprice,shakePrice,cakePrice;
	    cin>>n>>e>>h>>omprice>>shakePrice>>cakePrice;
	    int ans=1e18;
	    for(int cake=0;cake<=n;cake++)
	    {
	        if(e<cake||h<cake)
	        {
	            break;
	        }
	        int om=(e-cake)/2;
	        int shake=(h-cake)/3;
	        
	        if(cake+om+shake<n)
	         continue;
             int omreq,shakereq;
	        
	        if(shakePrice>omprice)
	        {
	        omreq=min(n-cake,om);
	        shakereq=n-cake-omreq;
	        }
	        if(shakePrice<omprice)
	        {
	            shakereq=min(n-cake,shake);
	            omreq=n-cake-shakereq;
	        }
	        
	        int price=(shakereq*shakePrice) + (omreq*omprice)+(cake*cakePrice);
	        ans=min(price,ans);
	    }
	   if(ans==1e18)
	   ans=-1;
	   
	   cout<<ans<<endl;
	}
 return 0;
}

Pay attention to compiler warnings (the last two in particular):

Compiling abhinav_700-COLGLF4.cpp
+ g++ -std=c++14 abhinav_700-COLGLF4.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
abhinav_700-COLGLF4.cpp: In function ‘int main()’:
abhinav_700-COLGLF4.cpp:40:13: warning: conversion to ‘double’ from ‘long long int’ may alter its value [-Wconversion]
     if(ans==1e18)
             ^~~~
abhinav_700-COLGLF4.cpp:37:29: warning: ‘shakereq’ may be used uninitialized in this function [-Wmaybe-uninitialized]
          int price=(shakereq*shakePrice) + (omreq*omprice)+(cake*cakePrice);
                    ~~~~~~~~~^~~~~~~~~~~~
abhinav_700-COLGLF4.cpp:37:50: warning: ‘omreq’ may be used uninitialized in this function [-Wmaybe-uninitialized]
          int price=(shakereq*shakePrice) + (omreq*omprice)+(cake*cakePrice);
                                            ~~~~~~^~~~~~~~~
+ set +x
Successful

1 Like

but codechef compiler is showing no syntax error or warnings

I’d recommend using g++ to compile your code locally, it will give you all the necessary details, I didn’t have a decent experience using online compilers (there probably is an online workaround that I’m not aware of).

From what I can see in your code, you’re missing a simple case when doing if statements so you leave two variables uninitialized, but I didn’t analyze your code too much, so that may not be the only mistake.

1 Like

what are two warnings trying to say me :sweat_smile: :sweat_smile: :sweat_smile:
I can’t see any mistake in declaring them

In case of equality, you don’t assign a value to these variables, so they stay uninitialized.

EDIT: Sentence sounded weird when I used “both of”…

1 Like

ok so you mean that if shakeprice=omprice
they will stay uninitialised
that is why this warning is shown

now i got correct answer by corrcecting that condition

thanks alot for helping guys :grinning: :grinning: :grinning:

2 Likes

Just a little tip when using Discuss. You can use ‘edit’ button in order to reduce the number of messages and simply keep the forum clean. :slight_smile:

No problem, I’m sure both of us are glad we could help. :wink:

1 Like