arrays

#include <iostream>
#include <conio.h>
#include <cstdlib>
using namespace std;
int main ()
{

char ans;
do
{
int i, n, sum;
int array [100];
cout << "Enter array size: ";
cin >> n;
cout << "Enter " << n << " numbers: " << endl;
for (i=0; i<n; i++)
{
cin >> array [i];
}

sum = 0;
for (i=0; i<n; i++)
{
sum = sum + array [i];
}

cout << endl;
cout << "Sum is: " << sum << " " << endl;
cout << endl;


int amount, a,b,c,d,e,f,g;
int result; // initial value unidentified
int var1 = 1000; //initial value = 1000
int var2 = 500; //initial value = 500
int var3 = 100; //initial value = 100
int var4 = 50; //initial value = 50
int var5 = 20; //initial value = 20
int var6 = 10; //initial value = 10
int var7 = 1; //initial value = 1


for (i=0; i<n; i++)
{
a = array [i]/var1;
if (a!=0)
cout << "1000 : "<< a << endl;
}

for (i=0; i<n; i++)
{
b = (array [i]%var1)/var2;
if (b!=0)
cout << "500 : "<< b << endl;
}

for (i=0; i<n; i++)
{
c = (array [i]%var1)%var2/var3;
if (c!=0)
cout << "100 : "<< c << endl;
}

for (i=0; i<n; i++)
{
d = (array [i]%var1)%var2%var3/var4;
if (d!=0)
cout << "50 : "<< d << endl;
}

for (i=0; i<n; i++)
{
e =(array [i]%var1)%var2%var3%var4/var5;
if (e!=0)
cout << "20 : "<< e << endl;
}

for (i=0; i<n; i++)
{
f = (array [i]%var1)%var2%var3%var4%var5/var6;
if (f!=0)
cout << "10 : "<< f << endl;
}

for (i=0; i<n; i++)
{
g = (array [i]%var1)%var2%var3%var4%var5%var6/var7;
if (g!=0)
cout << "1 : "<< g << endl;
}
cout << "Do you want to try again? ";
cin >> ans;
system("cls");
}
while (ans == 'y');

getch ();

}



*********************
example output:

Enter array size: 3
Enter 3 numbers:
1000
1000
1000

Sum is: 3000

1000: 1
1000: 1
1000: 1

*************
what I want to do is simply like this
1000: 3

Can you help me with my problem? Thanks in advance!
First when you hit the new topic button you will see that the code needs to go between the two code tags.

Instead of printing each item sum it and print the sums after the loop exits. I have not implemented it but that is how you might do it.

Thus instead of
cout << "1000 : "<< a << endl;
you might have aCount++;

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <conio.h>
#include <cstdlib>
using namespace std;
int main ()
{

    char ans;
    do
    {
        int i, n, sum;
        int array [100];
        cout << "Enter array size: ";
        cin >> n;
        cout << "Enter " << n << " numbers: " << endl;
        for (i=0; i<n; i++)
        {
            cin >> array [i];
        }

        sum = 0;
        for (i=0; i<n; i++)
        {
            sum = sum + array [i];
        }

        cout << endl;
        cout << "Sum is: " << sum << " " << endl;
        cout << endl;


        int amount, a,b,c,d,e,f,g;
        int result;       // initial value unidentified
        int var1 = 1000;  //initial value = 1000
        int var2 = 500;   //initial value = 500
        int var3 = 100;   //initial value = 100
        int var4 = 50;    //initial value = 50
        int var5 = 20;    //initial value = 20
        int var6 = 10;    //initial value = 10
        int var7 = 1;     //initial value = 1


        for (i=0; i<n; i++)
        {
            a = array [i]/var1;
            if (a!=0)
                cout << "1000 : "<< a << endl;

            b = (array [i]%var1)/var2;
            if (b!=0)
                cout << "500 : "<< b << endl;

            c = (array [i]%var1)%var2/var3;
            if (c!=0)
                cout << "100 : "<< c << endl;

            d = (array [i]%var1)%var2%var3/var4;
            if (d!=0)
                cout << "50 : "<< d << endl;

            e =(array [i]%var1)%var2%var3%var4/var5;
            if (e!=0)
                cout << "20 : "<< e << endl;

            f = (array [i]%var1)%var2%var3%var4%var5/var6;
            if (f!=0)
                cout << "10 : "<< f << endl;

            g = (array [i]%var1)%var2%var3%var4%var5%var6/var7;
            if (g!=0)
                cout << "1 : "<< g << endl;

            cout << endl << "------------" << endl;
        }
        cout << "Do you want to try again? ";
        cin >> ans;
        //system("cls");
    }while (ans == 'y');

    // getch ();
    return 0;

}
Last edited on
Topic archived. No new replies allowed.