C++ ARRAY

This code isnt finished yet. But is this some kind near to this program?


Input year level and age: 1 16
Input another? Y/N: Y
Input year level and age: 5 22
Input another? Y/N: Y
Input year level and age: 3 18
Input another? Y/N: Y
Input year level and age: 1 17
Input another? Y/N: N

Summary Report

16-17 18-19 20-21 22-23
1st year 2 0 0 0
2nd year 0 0 0 0
3rd year 0 1 0 0
4th year 0 0 0 0
5th year 0 0 0 1

Can someone help me with my codes? using int, char, and a looping?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  #include <iostream>
using namespace std;

void main()
{

	int array2d [5][4] = {0};
	int a, b;
	char choice;

	cout<<"Input year level and age: ";
	cin>>a;
	cin>>b;

	for (b=0;b<a;b++)

	for (a=0;a>4;a++)
		cout<<array2d[a]<<endl;
		a++;
Last edited on
both the for loops are wrong (I didn't realize what you are doing in them and the fact that writing like that
1
2
3
	for (a=0;a>4;a++)
		cout<<array2d[a]<<endl;
		a++;

don't mean that a++; is in the loop (it is in fact outside)
and also you have already increased it in the for
and the syntax cout<<array2d[a]<<endl; prints a pointer which is like for example AB15CAD5)

you should remove the loops and replace them by something like
array2d[a][b]++;
and you should enclose the whole main function except the 7th line with a do while loop which asks at the end for choice and loops as long as your answer is 'Y' and stops if 'N'
closed account (ivDwAqkS)
can you give us the exercise and maybe we could help you out. I did the program but I think its too long and you will not understand anything I saw you don't know array very well. AND ITS SO CONFUSING BECAUSE YOU HAVE 8 AGES WHEN IT SHOULD BE 4
Last edited on
closed account (j3Rz8vqX)
A possible example:
http://pastie.org/8689294
heres a quick program i made to do this.
Its a bit of a mess but it does the job.

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

using namespace std;


int main()
{
	int LevelAge[5][4] = {0};
	int age(0), level(0);
	bool quit = 0;
	int selection = 0;

    while (quit == 0)
    {
        cout << "1: Add to table \n2: Show Table \n3: quit \nInput selection: ";
        cin >> selection;
        cout << endl;
        switch(selection)
        {
            case 1:
            {
                cout<<"Input year level and age" << endl << "Level: ";
                cin>>level;
                cout << "Age: ";
                cin>>age;
                switch (age)
                {
                    case 16:
                    case 17:
                        LevelAge[level][0]++;
                    break;
                    case 18:
                    case 19:
                        LevelAge[level][1]++;
                    break;
                    case 20:
                    case 21:
                        LevelAge[level][2]++;
                    break;
                    case 22:
                    case 23:
                        LevelAge[level][3]++;
                    break;
                    default:
                        cout << "incorrect selection \n";
                }
            }
            break;
            case 2:
            {
                cout << "\n\n\t\t\t   Summary report\n\t\t16-17 \t18-19 \t20-21 \t22-23\n";
                cout << "1st Year\t";
                for(int i = 0; i < 4; i++){cout << LevelAge[0][i] << "\t" ;}
                cout << "\n 2nd Year\t";
                for(int i = 0; i < 4; i++){cout << LevelAge[1][i] << "\t" ;}
                cout << "\n 3rd Year\t";
                for(int i = 0; i < 4; i++){cout << LevelAge[2][i] << "\t" ;}
                cout << "\n 4th Year\t";
                for(int i = 0; i < 4; i++){cout << LevelAge[3][i] << "\t" ;}
                cout << "\n 5th Year\t";
                for(int i = 0; i < 4; i++){cout << LevelAge[4][i] << "\t" ;}
                cout << endl << endl ;
            }
            break;
            case 3:
                quit = 1;
            break;
            default:
                cout << "Incorrect selection! \n";

        }

    }

}

You can probably clean it up
If you dont understand anything just ask :)
Hope it helps
closed account (ivDwAqkS)
HERE IS THE SIMPLEST WAY YOU COULD UNDERSTAND THIS PROGRAM I COUNDLT DO IT SIMPLER
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;

int years[5][4];

int main(){
    int i, j;
    int value1=0, value11=0, value21=0, value31=0;
    int value2=0, value12=0, value22=0, value32=0;
    int value3=0, value13=0, value23=0, value33=0;
    int value4=0, value14=0, value24=0, value34=0;
    int value5=0, value15=0, value25=0, value35=0;
    char choise;
    while(true){
        cout << "Input year level: ";
        cin >> i;
        while(i >= 6){
            cout << "Please Enter a Year from 1 to 5: ";
            cin >> i;
        }
        cout << "Input age: ";
        cin >> j;
        while(j <=15 || j >=20){
            cout << "Please Enter an Age from 16 to 19: ";
            cin >> j;
        }

        if(i==1){
            if(j==16){
                years[0][0]=value1+=1;
            }
            if(j==17){
                years[0][1]=value11+=1;
            }
            if(j==18){
                years[0][2]=value21+=1;
            }
            if(j==19){
                years[0][3]=value31+=1;
            }
        }
        if(i==2){
            if(j==16){
                years[1][0]=value2+=1;
            }
            if(j==17){
                years[1][1]=value12+=1;
            }
            if(j==18){
                years[1][2]=value22+=1;
            }
            if(j==19){
                years[1][3]=value32+=1;
            }
        }
        if(i==3){
            if(j==16){
                years[2][0]=value3+=1;
            }
            if(j==17){
                years[2][1]=value13+=1;
            }
            if(j==18){
                years[2][2]=value23+=1;
            }
            if(j==19){
                years[2][3]=value33+=1;
            }
        }
        if(i==4){
            if(j==16){
                years[3][0]=value4+=1;
            }
            if(j==17){
                years[3][1]=value14+=1;
            }
            if(j==18){
                years[3][2]=value24+=1;
            }
            if(j==19){
                years[3][3]=value34+=1;
            }
        }
        if(i==5){
            if(j==16){
                years[4][0]=value5+=1;
            }
            if(j==17){
                years[4][1]=value15+=1;
            }
            if(j==18){
                years[4][2]=value25+=1;
            }
            if(j==19){
                years[4][3]=value35+=1;
            }
        }

        cout << "Would you like to continue?(y/n)";
        cin >> choise;
        if(choise=='n')
            break;
    }
//Mullti-dimentional array print
    cout << endl << "\t\tSummary Report"<<endl<<endl;
    cout << "Ages:\t 16  17  18  19"<<endl;
    for(i=0; i < 5; i++){
        cout << endl << "Year " << i+1 << ": ";
        for(j=0; j < 4; j++){
            if(i==0){
                if(j==0){
                    years[i][j]=value1;
                    cout << " " << years[i][j] << "  ";
                }
                if(j==1){
                    years[i][j]=value11;
                    cout << " " << years[i][j] << "  ";
                }
                if(j==2){
                    years[i][j]=value21;
                    cout << " " << years[i][j] << "  ";
                }
                if(j==3){
                    years[i][j]=value31;
                    cout << " " << years[i][j] << "  ";
                }
            }
            if(i==1){
                if(j==0){
                    years[i][j]=value2;
                    cout << " " << years[i][j] << "  ";
                }
                if(j==1){
                    years[i][j]=value12;
                    cout << " " << years[i][j] << "  ";
                }
                if(j==2){
                    years[i][j]=value22;
                    cout << " " << years[i][j] << "  ";
                }
                if(j==3){
                    years[i][j]=value32;
                    cout << " " << years[i][j] << "  ";
                }
            }
            if(i==2){
                if(j==0){
                    years[i][j]=value3;
                    cout << " " << years[i][j] << "  ";
                }
                if(j==1){
                    years[i][j]=value13;
                    cout << " " << years[i][j] << "  ";
                }
                if(j==2){
                    years[i][j]=value23;
                    cout << " " << years[i][j] << "  ";
                }
                if(j==3){
                    years[i][j]=value33;
                    cout << " " << years[i][j] << "  ";
                }
            }
            if(i==3){
                if(j==0){
                    years[i][j]=value4;
                    cout << " " << years[i][j] << "  ";
                }
                if(j==1){
                    years[i][j]=value14;
                    cout << " " << years[i][j] << "  ";
                }
                if(j==2){
                    years[i][j]=value24;
                    cout << " " << years[i][j] << "  ";
                }
                if(j==3){
                    years[i][j]=value34;
                    cout << " " << years[i][j] << "  ";
                }
            }
            if(i==4){
                if(j==0){
                    years[i][j]=value5;
                    cout << " " << years[i][j] << "  ";
                }
                if(j==1){
                    years[i][j]=value15;
                    cout << " " << years[i][j] << "  ";
                }
                if(j==2){
                    years[i][j]=value25;
                    cout << " " << years[i][j] << "  ";
                }
                if(j==3){
                    years[i][j]=value35;
                    cout << " " << years[i][j] << "  ";
                }
            }


        }
    }

    return 0;
}
Last edited on
No offence xdmelasxx but i dont think that is the simplest way to show it, its actually quite difficult to follow.
closed account (ivDwAqkS)
Jidder what I meant with the simplest way is that he does not have to use difficult codes :D he said he needed char and multi dimentional array that's what I did , and its easy to understand for beginners.
Last edited on
Jidder, what you did was really a 95% of the work but there is something wrong with the cases. Here is what i did to it. I made it a negative and it still works 100%


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
                    case 16:
                    case 17:
                        LevelAge[level][-4]++;
                    break;
                    case 18:
                    case 19:
                        LevelAge[level][-3]++;
                    break;
                    case 20:
                    case 21:
                        LevelAge[level][-2]++;
                    break;
                    case 22:
                    case 23:
                        LevelAge[level][-1]++;
                    break;
                    default:
                        cout << "incorrect selection \n";
xdmelasxx, i really appreciated your help. its really a bit long and a complex one. but that is what im looking for ;)
The cases are fine.
I dont know why you changed them because they worked as intended.
If you use negative numbers it reads back from the end of the array
so in an array of 4 values [0] and [-4] are equivalent.

Edit: After reading up on it reads backwards that many elements in the array from where the pointer currently is in the array granted it is atleast that many elements into the array to begin with.
Which i think is what NT3 is saying in the next comment.

Also i have deleted my last comment as it does not contribute to the problem.
Last edited on
jidder wrote:
If you use negative numbers it reads back from the end of the array

What??? Are you kidding? For starters, most of the time your array has already degenerated into a pointer, so it doesn't know where the end of the array is. This is the most ridiculous thing I've heard, don't use negative array indices. Here is an example: http://ideone.com/aiBf75.

The use of negative array indices is used to go back from the array, assuming the pointer is already inside the middle of the array. However, even that is undefined behaviour, because the array index is an unsigned integer value (32bit), which means that the value taken only works on 32 bit address spaces (for the wrap around effect). When ported to a 64 bit machine, or an implementation that doesn't use 32 bit address spaces, this will fail.

EDIT:
Here is a quick article explaining it: http://www.devx.com/tips/Tip/41349
Last edited on
Topic archived. No new replies allowed.