This question already has answers here:
Why does dividing two int not yield the right value when assigned to double?
(10 answers)
Closed 6 years ago.
i am learning c++ , student of first semester,
I am trying to divide 2 arrays in loop and store its result in 3rd array that's not working.All other operations work except division following is my code
#include<iostream>
using namespace std;
int main(){
int arr[6]={10,20,30,40,50,06};
int arr2[6]={10,20,20,30,40,50};
int input;
cout<<"please enter 1 for addition.... "<<endl;
cout<<"please enter 2 for subtraction.... "<<endl;
cout<<"please enter 3 for multiplication.... "<<endl;
cout<<"please enter 4 for division.... "<<endl;
cout<<"please enter 5 for %age...."<<endl;
cin>>input;
float arr3[6];
int i,j,k,l,m;
switch(input){
case 1:
for(i=0;i<6;i++){
arr3[i]=arr[i]+arr2[i] ;
cout<<arr3[i]<<endl;
}
break;
case 2:
for(j=0;j<6;j++){
arr3[j]=arr[j]-arr2[j];
cout<<arr3[j]<<endl;
}
break;
case 3:
for(k=0;k<6;k++){
arr3[k]=arr[k]*arr2[k];
cout<<arr3[k]<<endl;
}
break;
case 4:
for(l=0;l<6;l++){
arr3[l]=arr[l]/arr2[l];
cout<<arr3[l]<<endl;
}
break;
case 5:
for(m=0;m<6;m++){
arr3[m]=arr[m]/(arr[m]+arr2[m]);
cout<<arr3[m]<<endl;
}
break;
}
}
for division output is :
1
1
1
1
1
0
i tired to set its data type as dobule and float both but its not working need your help please.
You need to cast the operands before you divide, or else it will use integer division, which has its decimal shaved off. Using c-style casts, it would look like:
arr3[l]= (double)arr[l] / (double)arr2[l];
Note, C++ has its own casting syntax, but it's been awhile since I've used them, and can't remember them exactly. I think it would look like:
arr3[l] = static_cast<double>(arr[l])
/ static_cast<double>(arr2[l]);
Related
This question already has answers here:
Is there ever a need for a "do {...} while ( )" loop?
(19 answers)
Why two loops in Programming Languages? [closed]
(4 answers)
Closed 3 months ago.
Im making a tip calculator and want to generate an error if the user enters a negative number of a character. Although the program is working, my question is that why is generating an error for character even when I haven't put any such condition yet?
//Tip Calculator
#include<iostream>
using namespace std;
int main()
{
//Declaring Variables
float BillAmount, TipRate, Tip, TotalBill;
//Taking input for bill amount and generating an error if its negative/charcter
cout<<"Enter the Bill Amount: "; cin>>BillAmount;
if (BillAmount<=0)
{
cout<<"Enter a Valid Amount"<<endl;
exit(0);
}
//Taking input for tip rate and generating an error if its negative/character
cout<<"Enter the Tip Rate: "; cin>>TipRate;
if (TipRate<=0)
{
cout<<"Enter a Valid Amount"<<endl;
exit(0);
}
//Calculating and printing the tip to 2dp
Tip = (TipRate/100)*BillAmount;
cout<<"Tip: $";printf("%.2f",Tip);cout<<endl;
//Calculating the total amount and printing it to 2dp
TotalBill = BillAmount + Tip;
cout<<"Total Bill: $";printf("%.2f",TotalBill);cout<<endl;
}
Also, is there a way to restart the whole thing after the error?
As of now, I only wanted to generate an error over a negative value, however it is now giving the error "Enter a Valid Amount" on both negative values and upper/lowercase letters. Why is that?
The link to the problem is "https://www.codechef.com/OCT17/problems/PERFCONT"
I have worked out a solution to this problem but I am getting wrong answer.
My solution:
#include<iostream>
using namespace std;
int main(){
long long int hard,cakewalk,t,temp;
long long int n,p;
cin>>t;
while(t--){
hard = cakewalk = 0;
cin>>n;
cin>>p;
while(n--){
cin>>temp;
if(temp<=(p/10))
hard++;
if(temp>=(p/2))
cakewalk++;
if(hard>2 || cakewalk>1){
break;
}
}
if(hard==2 && cakewalk ==1){
cout<<"yes"<<endl;
}
else{
cout<<"no"<<endl;
}
}
return 0;
}
As I have gathered we have to calculate the number of Hard and Cakewalk type problems and if there exactly 2 and 1 respectively, it's a balanced contest.
Kindly help me in solving this.
You are getting WA because you are breaking when hard > 2 || cakewalk > 1 without reading the whole input line. So, after it breaks and gives the correct input for the given test case, but it will fail because the number that is next inputted is technically not the input for the next test case. It is the remaining part of first test case.
For input:
2
4 100
1 1 1 1
2 100
1 50
For the first test case, you skip taking input after reading 1 1 1 from line 3. So, the next input you take is 1 (considering it to be n) whereas n should be 2 for next test case.
Removing this should work:
if(hard>2 || cakewalk>1){
break;
}
I am researching about the Balloon Sort because it was one of my assignments, but the Google give me one Balloon Sort link sample only and the rest was Bubble Sort.
I compiled the code in Dev C++ and said that it has some error...
Here's [a link] (http://www.codemiles.com/c-examples/balloon-sort-algorithm-c-implementation-code-sorting-array-t10823.html) ! That Google gave me...
here is the code...
#include<iostream>
using namespace std;
void balloon()
{ int num, N[10], x, y, z,temp;
clrscr();
cout<<"How many number would you like to sort? ";
cin>>num;
cout<<"Input the "<<num<<" numbers:"<<endl;
for(x=0;x<num;x++)
cin>>N[x];
for(x=0;x<num;x++)
{
for(y=0;y<num-x;y++)
{ if(N[x] > N[x+y])
{ temp=N[x];
N[x] =N[x+y];
N[x+y]=temp;
}
}
cout<<"pass "<<x+1<<"] ";
for(z=0;z<num;z++)
{
cout<<setw(5)<<N[z];
}
cout<<endl;
}
}
Error Picture Link
Can you help me how to code the Balloon Sort in C++ with some explanations... Thanks in advance!
It will have some error because the compiler will search the int main() so change the void balloon, and remove the clrsrc();
Since you used the setw(), you must use the #include <iomanip>
The header is part of the Input/output library of the C++ Standard Library. It defines the manipulator functions resetiosflags(), setiosflags(), setbase(), setfill(), setprecision(), and setw(). These functions may be conveniently used by C++ programs to affect the state of iostream objects.
And lastly you must make the cout<<setw(5)<<N[z]; into cout<<std::setw(5)<<N[z];
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{ int num, N[10], x, y, z,temp;
cout<<"How many number would you like to sort? ";
cin>>num;
cout<<"Input the "<<num<<" numbers:"<<endl;
for(x=0;x<num;x++)
cin>>N[x];
for(x=0;x<num;x++)
{
for(y=0;y<num-x;y++)
{ if(N[x] > N[x+y])
{ temp=N[x];
N[x] =N[x+y];
N[x+y]=temp;
}
}
cout<<"pass "<<x+1<<"] ";
for(z=0;z<num;z++)
{
cout<<std::setw(5)<<N[z];
}
cout<<endl;
}
}
and if you run it... here is my sample output
How many number would you like to sort? 5
Input the 5 numbers:
8
2
4
9
0
pass 1] 0 8 4 9 2
pass 2] 0 2 8 9 4
pass 3] 0 2 4 9 8
pass 4] 0 2 4 8 9
pass 5] 0 2 4 8 9
--------------------------------
Process exited after 8.305 seconds with return value 0
Press any key to continue . . .
Hope this works on your assignment! Good Luck!
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have found 4 tasks on recruitcoders.com and
I have completed all of them, but in the first one i have scored only 1/10:
Write a program that works as a simple calculator that supports five operations: addition, subtraction, multiplication, division and modulo.
Input:
There is an unknown number of tests. Each test consists of one-character symbol which corresponds to specific operation (+ addition, - subtraction, * multiplication, / division and % modulo) and two following integers. Each test will be separated by spaces and followed by a newline. Number of tests doesn't exceed 100 and the result is less than 2^31. You can assume that there is no situation in which you would have to divide by 0.
Output:
For each test you should print a single number being the result of each operation.
Example:
Input:
+ 7 9
- 0 4
* 5 6
/ 8 3
% 5 2
Output:
16
-4
30
2
1
MyCode:
#include <iostream>
using namespace std;
int fcount(char, int, int);
int main() {
char znak;
long a, b;
long* wynik=new long[100];
for(char i=0;i<100;i++){
cin>>znak>>a>>b;
wynik[i]=fcount(znak,a,b);
}
for(char i=0;i<100;i++)
cout<<wynik[i]<<endl;
return 0;
}
int fcount(char znak, int a, int b){
switch(znak){
case '+':
return a+b;
case '-':
return a-b;
case '*':
return a*b;
case '/':
return a/b;
case '%':
return a%b;
}
}
THIS CODE IS WORKING FINE, IT IS JUST UNDERRATED BY RECRUITCODERS (1/10)
I am not asking you for better code, I just wonder where am I loosing so many points in such an easy task? Any suggestions? I have completed all 4 tasks scoring 28/40 total (1/10, 10/10, 10/10, 7/10), so the task with score 1/10 is a pain in a** for me :/
The requirement says that there is an unknown number of tests, but you are assuming exactly 100 tests. Change it to:
while (cin >> znak >> a >> b)
cout << fcount(znak, a, b) << endl;
The user will input 2 equations then solving for its iteration (sorry for my English). The problem that the loop is not being executed. The code should break out when the value of et is less than the value of g.
The code:
#include <iostream>
#include<stdlib.h>
using namespace std;
long double g=0.0010;
int main()
{
long double xe,ye,et,k,x,y,x1,x2,y1,y2,c1,c2,a,b;
//for the input
cout<<"EQUATION 1:\n";
cout<<"Input your desired numerical coefficient for x:"<<endl;
cin>>x1;
cout<<"Input your desired numerical coefficient for y:"<<endl;
cin>>y1;
cout<< "Input your constant's value:"<<endl;
cin>>c1;
system("CLS");
cout<<"EQUATION 2:\n";
cout<<"Input your desired numerical coefficient for x:"<<endl;
cin>>x2;
cout<<"Input your desired numerical coefficient for y:"<<endl;
cin>>y2;
cout<< "Input your constant's value:"<<endl;
cin>>c2;
system("CLS");
//to show the equation made
cout<<"Your EQUATION 1 is:\n"<<x1<<"x + <"<<y1<<"y)"<<" = "<<c1<<endl<<endl;
cout<<"Your EQUATION 2 is:\n"<<x2<<"x + ("<<y2<<"y)"<<" = "<<c2<<endl<<endl;
//first value of x and y
x=c1/x1;
y=(c2)/y2;
//show the values
cout<<"\nx="<<x<<endl;
cout<<"y="<<y<<endl;
//this is where the iteration starts
for(k=1;g>et;k++)
{
a=(c1+y)/x1;
b=(c2-x)/y2;
xe=((a-y)/a)*-1;
ye=((b-x)/b);
et=((xe+ye)/2);
cout<<"k="<<k;
cout<<"\nx="<<a<<endl;
cout<<"y="<<b<<endl;
cout<<"\nxe="<<xe;
cout<<"\nye="<<ye;
cout<<"\net="<<et<<endl;
}
return 0;
}
Shouldn't you be using
while (std::abs(et) > g)
or something like that instead ?
You should use more whitespace to make it easier to see what you're doing. Seriously, this is the main problem.
The termination condition should be when the maximum error is below a certain limit, because then all variables have converged "enough." As it is, you're taking the sum of the rates of change, which may cause premature termination if they happen to be of opposite sign, or uneven degrees of convergence otherwise.
Negating xe does not help because in the general case they can overshoot and reverse sign.