switch statement

Currently my code is not working, Im trying to count the total of each remainder from the 20 numbers user inputs. How shall i improve it so that i can total them up using switch statement.


#include <iostream>

using namespace std;

int main()
{
int i, x[20], count;
cout << "Enter 20 integer numbers from 0 to 99: " <<endl;
for (i=1;i<=20;i++)
{
cout << "Input " << i <<":";
cin >> x[i];
}

double remainder ;

for(i=1; i<=20; i++)
{
switch (x[i] % 8)
{
case 0 :
remainder = x[i] % 8 ;
count++ ;
cout << "Total number with remainder zero is " << count << endl ;
break;

case 1 :
remainder = x[i] % 8 ;
count++ ;
cout << "Total number with remainder one is " << count << endl ;
break;

case 2 :
remainder = x[i] % 8 ;
count++ ;
cout << "Total number with remainder two is " << count << endl ;
break;

case 3 :
remainder = x[i] % 8 ;
count++ ;
cout << "Total number with remainder three is " << count <<endl ;
break;

default :
remainder = x[i] % 8 ;
count++ ;
cout << "Total number with remainder other than zero, one, two and three is " << count << endl ;
}
}
return 0 ;
}

Example how the output should look:
Total number with remainder 0 is 4.
Total number with remainder 1 is 6.
Total number with remainder 2 is 5.
Total number with remainder 3 is 3.
Total number of other remainder is 2.
Hi,

==>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
using namespace std;
int main()
{
int i, x[20 + 1];
int remainder_zero = 0;
int remainder_one = 0;
int remainder_two = 0;
int remainder_three = 0;
int remainder_other = 0;

cout << "Enter 20 integer numbers from 0 to 99: " << endl;
for (i=1;i<=20;i++)
{
cout << "Input " << i << ":";
cin >> x[i];
}
double remainder;
for(i=1; i<=20; i++)
{
switch (x[i] % 8)
{
case 0 :
remainder = x[i] % 8 ;
remainder_zero += 1;

break;
case 1 :
remainder = x[i] % 8 ;
remainder_one += 1;

break;
case 2 :
remainder = x[i] % 8 ;
remainder_two += 1;

break;
case 3 :
remainder = x[i] % 8 ;
remainder_three += 1;

break;
default :
remainder = x[i] % 8 ;
remainder_other += 1;
}
}
cout << "Total number with remainder zero is " << remainder_zero << endl;
cout << "Total number with remainder one is " << remainder_one << endl;
cout << "Total number with remainder two is " << remainder_two << endl;
cout << "Total number with remainder three is " << remainder_three << endl;
cout << "Total number with remainder other than zero, one, two and three is " << remainder_other << endl ;

return 0;
}
http://www.cplusplus.com/forum/beginner/194552/
TheIdeasMan wrote:
Just a note for the future :+)

There also shouldn't be 2 topics about essentially the same subject. There is a risk that the same things will be said in both, possibly making it a time waster for those who reply.
thanks for improving it. But now when i combine them with rest of my codes. it has some errors. First is the max number is giving some huge number like 1354628. second my prime number code is showing 1 has prime number where it should not. I hope someone can pin point where is the mistake

#include <iostream>

using namespace std;

int main()
{
int i,t, max, min, x[20 + 1], even, odd, prime;
even = 0;
odd = 0;
cout << "Enter 20 integer numbers from 0 to 99: "<<endl;
for (i=1;i<=20;i++)
{
cout << "Input " << i <<":";
cin >> x[i];
}
min=x[0];
max=x[0];
for(int i=0;i<20;i++)
{
if(min>x[i])
{
min=x[i];
}
else if(max<x[i])
{
max = x[i];
}

}
cout<<"\nMax is "<< max <<endl;
cout<<"\nMin is "<< min <<endl;

cout << "\nPrime numbers are: " << endl ;
prime=1;
for (i=2; i<=20 ; i++)
{
for(t=2;t<x[i];t++)
{
if(x[i]%t==0)
{
prime=0;
}
}

if(prime==1)
{
cout << x[i] << endl;
}
prime=1;
}

for(i=1; i<=20; i++)
{
if(x[i]% 2 == 0)
{
even++;
}
else
{
odd++;
}

}

cout << "Number of odd numbers: " << odd << "\n";
cout << "Number of even numbers: " << even << "\n";

int remainder_zero = 0;
int remainder_one = 0;
int remainder_two = 0;
int remainder_three = 0;
int remainder_other = 0;
double remainder;
for(i=1; i<=20; i++)
{
switch (x[i] % 8)
{
case 0 :
remainder = x[i] % 8 ;
remainder_zero += 1;

break;
case 1 :
remainder = x[i] % 8 ;
remainder_one += 1;

break;
case 2 :
remainder = x[i] % 8 ;
remainder_two += 1;

break;
case 3 :
remainder = x[i] % 8 ;
remainder_three += 1;

break;
default :
remainder = x[i] % 8 ;
remainder_other += 1;
}
}
cout << "Total number with remainder zero is " << remainder_zero << endl;
cout << "Total number with remainder one is " << remainder_one << endl;
cout << "Total number with remainder two is " << remainder_two << endl;
cout << "Total number with remainder three is " << remainder_three << endl;
cout << "Total number with remainder other than zero, one, two and three is " << remainder_other << endl ;


return 0 ;
}
Topic archived. No new replies allowed.