Using switch statement to create a program that total up remainder when divide by 8

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.
Last edited on
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;
}
Does that help you? :)
Ya it does. Thanks alot but i came across different issue when i combine them together. 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 ;
}
Well,
Firstly, initialize your necessary variables. Variables usually start with random (garbage) values if left uninitialized.

So :
int i, t, max, min, x[20 + 1], even, odd, prime;

==>
int i, t = 0, max = 0, min = 0, x[20 + 1], even = 0, odd = 0, prime = 0;
Its still showing some huge numbers... i have already initialize them

#include <iostream>

using namespace std;

int main()
{
int i,t, max, min, x[20 + 1], even, odd, prime;
even = 0;
odd = 0;
max = 0;
min = 0;
prime = 0;
t = 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=1;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 ;
}
What are the huge numbers?
Can you display them again?
1
2
3
4
5
6
7
8
9
10
11
12
13
min=x[0];
max=x[0];
for(int i=1;i<=20;i++)
{
if(min>x[i])
{
min=x[i];
}
else if(max<x[i])
{
max = x[i];
}
}


Should be :
1
2
3
4
5
6
7
8
9
10
11
12
13
min=x[1];
max=x[1];
for(int i=1;i<=20;i++)
{
if(min>x[i])
{
min=x[i];
}
else if(max<x[i])
{
max = x[i];
}
}
Does that help? :)
YES!! it works now... Thanks.. can you explain y it has to be 1 not 0? is it because i use x[20 + 1] ? Between, i still 1 and small issue, my prime number part coding it shows 1 has prime when entered. how shall i improvise tht?
@kugen

Pleas always use code tags, and indent your code: http://www.cplusplus.com/articles/z13hAqkS/


closed account 5a8Ym39o6 wrote:
x[20 + 1]


1
2
3
4
5
6
const unsigned int size = 20;
int x[size];

for (unsigned int i = 0; i < size; ++i ) {

}



That is the idiomatic format of the for loop. Remember array subscripts start at zero, and finish at size -1. At the moment the array only has 19 usable numbers.

x[20 + 1] is a hack that caused problems. Also realise that just because a program works doesn't necessarily mean that is correct.

Good Luck !!

@closed account 5a8Ym39o6 (843)

With about 300 usable posts under your belt, you might not promote such solutions.
Last edited on
Thanks for the advise. ill keep that in mind. And i will try to improvise them. Thanks again to pointing it out
Topic archived. No new replies allowed.