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!
I don't understand what you want
What I want to happen in my program is instead of this:

1000: 1
500: 1
50: 1
1: 6

1000: 2
100: 3
10: 1
1: 2

1000: 1
500: 1
10: 1
1: 1


The result should be like this. Can anyone help me out? Thanks!

1000: 4
500: 2
100: 3
50: 1
10: 2
1: 9
Last edited on
For what you are trying to do, I would suggest that instead of storing all the numbers that you just store the count of each specific number you are interested in:

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
#include <iostream>

using namespace std;

int main()
{
   char ans;

   struct Numbers
   {
      int value;
      int count;
   };

   do
   {
      Numbers array[] = {{1000, 0},
                                    {500, 0},
                                    {100, 0},
                                    {50, 0},
                                    {20, 0},
                                    {10, 0},
                                    {1, 0}};

      cout << "Enter how many numbers: ";
      int n;
      cin >> n;
      cout << "Enter " << n << " numbers:" << endl;

      for (int i = 0; i << n; ++i)
      {
          int num;
          cin >> num;
          switch (num)
          {
              case 1000:
                 array[0].count++;
                 break;
              case 500:
                 array[1].count++;
                 break;
              case 100:
                 array[2].count++;
                 break;
              case 50:
                 array[3].count++;
                 break;
              case 20:
                 array[4].count++;
                 break;
              case 10:
                 array[5].count++;
                 break;
              case 1:
                 array[6].count++;
                 break;
         }
      }

      int sum(0);

      for (int i = 0; i < sizeof(array)/sizeof(Numbers); ++i)
      {
          sum += array[i].value * array[i].count;
      }

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

      for (int i = 0; i < n; ++i)
      {
          if (array[i].count > 0)
             cout << array[i].value << ":" << array[i].count << endl;
      }

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

   return 0;
}


HTH
I think line #69 should read for (int i = 0; i < sizeof(array)/sizeof(Numbers); ++i) otherwise the index can go out of bounds.

Also, sum can be calculated in the first loop itself.
So modified code with minor tweaks

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
#include <iostream>

using namespace std;

int main()
{
   char ans = 'y';

   struct Numbers
   {
      int value;
      int count;
   };

   do
   {
      Numbers array[] = {{1000, 0},
                                    {500, 0},
                                    {100, 0},
                                    {50, 0},
                                    {20, 0},
                                    {10, 0},
                                    {1, 0}};

      cout << "Enter how many numbers: ";
      int n;
      cin >> n;
      cout << "Enter " << n << " numbers:" << endl;

      long sum(0);
      for (int i = 0; i << n; ++i)
      {
          int num;
          cin >> num;
          switch (num)
          {
              case 1000:
                 array[0].count++;
                 break;
              case 500:
                 array[1].count++;
                 break;
              case 100:
                 array[2].count++;
                 break;
              case 50:
                 array[3].count++;
                 break;
              case 20:
                 array[4].count++;
                 break;
              case 10:
                 array[5].count++;
                 break;
              case 1:
                 array[6].count++;
                 break;
         }

         sum += num;
      }

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

      for (int i = 0; i < sizeof(array)/sizeof(Numbers); ++i)
      {
          if (array[i].count > 0)
             cout << array[i].value << ":" << array[i].count << endl;
      }

       cout << "Do you want to try again? ";
       cin >> ans;
   } while (ans == 'y' || ans == 'Y');

   return 0;
}

Topic archived. No new replies allowed.