Simple C++ code to have fun and learn algorithm - c++

==
Code founded online in Chegg, however the output is not the one that was told. It supposed to give a ounce_left of 4, but is giving an 8.
Someone knows where is the bug?
==
In this problem, our task is to find the no_of _cans needed and the no_of_ounce left over.
The formula to calculate no_of_cans needed is: ceil(amount/12).
Formula to calculate no_of_ounce needed is: 12*ceil(amount/12)-amount.
Algorithm
Take input amount from user.
Divide amount/12 and store ceil of quotient in variable cans_needed.
Ounce_left will be calculated as 12*cans_needed-amount.
Print cans_needed.
Print ounce_left.
Stop.
Now let us try to implement the above algorithm using C++.
//header files used
#include<bits/stdc++.h>
using namespace std;
int main()
{
int amount;
//taking input from user
cout<<"Enter Amount in ounces:";
cin>>amount;
//finding no of cans needed
//if it is evenly divided by 12 then ceil(quotient)=quotient
//else ceil(quotient)=quotient+1
int cans_needed=ceil((double)amount/12);
//finding ounce_left
int ounce_left=amount%12;
cout<<"Cans Needed :"<<cans_needed<<endl; //printing cans_needed
cout<<"Ounce left: "<<ounce_left<<endl; //printing ounce_left
}

Related

I'm unsure exactly why the 'counter' variable isn't responding in the way I would like it to

Please bear with me since I am very new to C++ and coding in general.To the point where this is my second or third time using arrays. Here is the code in question:
#include <iostream>
using namespace std;
int main()
{
int day,month,counter;
int year[12]={31,28,31,30,31,30,31,31,30,31,30,31};
cout<<"Input a day of a month";
cin>>day;
cout<<"Input the month of the year";
cin>>month;
for(int x=0;x>12;x++)
{
cin>>year[x];
counter+=year[x];
}
cout<<"The Date is " <<day<<" / "<<month<<" and, "<<endl;
cout<<"The number of days until this date is reached again is:"<<counter<<endl;
}
I am trying to have the code show an imputed date by the user and the number of days until that date is reached again. However the counter variable is only showing 0. I am decently sure this is some form of logic error I have yet to find.
Your loop
for(int x=0;x>12;x++)
sets x to 0 and then checks if it's greater than 12, which it isn't, and thus ends doing nothing.

cout doesn't works properly on my program, can somebody help me?

enter image description hereI am using the STL in c++ and inside a bucle the cout doesn't prints correctly a float.
my program ads values to a vector and then passes it to a function to see if the condition exist, actually it works perfectly but just the cout doesn't word, I already tried using printf() but it gave the same result.
note:please algo give me feedback on my question is the first time i do one and English is not my natal language
my code:
#include<bits/stdc++.h>
#include<vector>
using namespace std;
void isthereanumber(vector<float> array);
int main(){
string ans;vector<float> array;float number;
do{
fflush(stdin);
cout<<"insert a value for the vector: "<<endl;
cin>>number;
array.push_back(number);
fflush(stdin);
cout<<"would you like to keep adding values to the vector? "<<endl;
getline(cin,ans);
}while(ans.compare("yes")==0);
isthereanumber(array);
return 0;
}
void isthereanumber(vector<float> array){
float suma =0;
for(vector<float>::iterator i=array.begin();i!=array.end();i++){
for(vector<float>::iterator j=array.begin();j!=array.end();j++){
if(i!=j){
suma = suma+array[*j];
}
}
if(suma=array[*i]){
cout<<"there is a number that the addition of every number in the array except the number is equal to the number \n";fflush(stdin);
cout<<"the number is: "<<suma;/*here is the cout that doesnt works properly or perhabs is something else i don't know*/
return;
}
}
cout<<"there is not a number with such a condition: ";
return;
}
As stated by cleggus already there are some issues present. Those need to be adressed first. After that there's a logical error in that suma just keeps growing.
Given the input 5,5,10 once we test for 10 we would like suma to be set to 0 again for it to work but it will be something like 30 now instead.
That can be solved by moving suma inside the outer loop.
Working example for input 5,5,10: https://godbolt.org/z/gHT6jg
I think you may have a couple of issues...
In your for loops you are creating iterators to the vector, but rather than just dereferencing them to access the indexed element you are dereferencing them and then using that as an index to the same vector.
Also Your last if statement has an assignment = rather than a comparison ==.
I believe this is closer to what you are trying to achieve (sorry I haven't had time to compile and check):
for(vector<float>::iterator i=array.begin();i!=array.end();i++){
for(vector<float>::iterator j=array.begin();j!=array.end();j++){
if(i!=j){
suma = suma+*j;
}
}
if(suma==*i){
cout<<"there is a number that the addition of every number in the array except the number is equal to the number \n";fflush(stdin);
cout<<"the number is: "<<suma;/*here is the cout that doesnt works properly or perhabs is something else i don't know*/
return;
}
}

Rotation of integers stored in an array

I have made a simple program of rotation of integers in a given array.
#include <iostream>
using namespace std;
int main()
{
int t;
cin>>t;
long i,j,n,k,l,m,x;
long a[1000000];
for(i=0;i<t;i++){
cin>>n;
cin>>k;
for(j=0;j<n;j++){
cin>>a[j];
}
for(m=0;m<k;m++){
x=a[0];
for(j=1;j<n;j++){
x=a[j]+x;
a[j]=x-a[j];
x=x-a[j];
}
a[0]=x;
}
for(j=0;j<n;j++){
cout<<a[j]<<" ";
}
cout<<endl;
}
return 0;
}
The question can be found here. My code processes the small inputs easily, but when the input reaches to the order of thousands it takes well over a second and it fails due to that. Any suggestions on how to solve this problem?
Some notes that can be used to speed things up:
Rotating by K, when K>N, is equivalent to rotating by K%N (as each rotation of N is an expensive no-op).
You don't actually need to generate the rotated array; you just need to print the values as they would appear in the rotated array.
In fact, you don't (necessarily) need to store all of the input in an array, but just the part that can't be printed until the last value in the array is read. (This would get the input and output intermingled if both were through the console, but not if at least one were a file.)
These can be used to turn your O(N^2) solution into an O(N) one.

C++ functions that takes multiple numbers and perform different jobs

I have an exercise where I should input multiple numbers which finish with zero and then perform different calculations with them. So far I have created a do..while-loop for storing the numbers. I´ve been told that this is possible to do without an array, if that´s wrong please tell me right away. The functions I need to do is to add all the numbers together, locate the highest number and the second highest number, and also the mid (mean) value of all the numbers.
I think there are different libraries I should use and there may be different options also.
Please help me to understand the different libraries and how to use them.
The search results I´ve found on the the web does not give me the answers I´m looking for because I could not find a similar problem to mine.
This is my code so far:
#include<iostream>
#include<string>
using namespace std;
int sumCalc (int);
int midValCalc (int);
int secHighCalc (int);
int highestCalc (int);
void printCalc ();
int main() {
int nums;
cout << "Input numbers, quit with 0: ";
do {
cin >> nums;
cout << nums << " "; // This is just to see the result
}
while (nums != 0);
return 0;
}
What is wrong with this add function?
int sumCalc (int total) {
total = 0;
while (nums != 0) {
total += nums;
}
return nums;
}
I don't think you need any unusual libraries for this.
Think about how you'd calculate these things mentally. For example, if you want to sum a list of numbers that I read off to you, you'd just keep track of the running total, and forget each number as you added it - so that only needs one variable. If you want to keep track of the greatest number entered, again, you simply remember the biggest one you've seen so far and compare new numbers as you get them.
You can probably solve these.
If you need to get the mean and highest and second-highest numbers you don't need an array(you don't need to store the numbers)
Essentially, you can keep track of the highest and second highest numbers that the user has entered, and if the user enters a number higher than those, you can adjust accordingly.
As for the mean average, you can also keep a running sum and count(# of numbers entered) which you can use to calculate the mean with whenever you need to.

dynamic programming: coin change

I have as an input:
the number of testcases
an amount of money
As output I need:
The number different coins we have and the value of each coin.
The program should determine whether there is a solution or not, so the output should be either a "yes" or a "no".
I wrote the program using dynamic programming, but it only works when I enter one testcase at a time If i write let's say 200 testcases at once, the output isn't always right.
I'm assuming that I have an issue with incorrectly saved state between test cases.
My question is, how could I solve this problem? I'm only asking for some advice.
Here's my code:
#include<iostream>
#include<stdio.h>
#include<string>
#define max_muenzwert 1000
using namespace std;
int coin[10];//max. 10 coins
int d[max_muenzwert][10];//max value of a coin und max. number of coins
int tabelle(int s,int k)//computes table
{
if(d[s][k]!=-1) return d[s][k];
d[s][k]=0;
for(int i=k;i<=9&&s>=coin[i];i++)
d[s][k]+=tabelle(s-coin[i],i);
return d[s][k];
}
int main()
{
int t;
for(cin>>t;t>0;t--)//number of testcases
{
int n; //value we are searching
scanf("%d",&n)==1;
int n1;
cin>>n1;//how many coins
for (int n2=0; n2<n1; n2++)
{
cin>>coin[n2];//value of coins
}
memset(d,-1,sizeof(d));//set table to -1
for(int i=0;i<=9;i++)
{
d[0][i]=1;//set only first row to 1
}
if(tabelle(n,0)>0) //if there's a solution
{
cout<<"yes"<<endl;
}
else //no solution
{
cout<<"no"<<endl;
}
}
//system("pause");
return 0;
}
As you can see you have variable number of coins, which you are taking input using this line: cin>>n1;//how many coins. But in the tabelle method you are always looping through 0 - 9, which is wrong. You should only loop through 0 - n1. Try this test case:
2
10
2
2 5
10
1
9
For the second test set your answer should be no but your program will say yes as it will find 5 in the second element of your coin array.
for(int i=k;i<=9&&s>=coin[i];i++)
d[s][k]+=tabelle(s-coin[i],i);
Here, if coin[i] < s, then the entire loop will break, while you only need to skip this coin.
P.S. Please bother yourself with proper code formatting.