2D Arrays

hey there. im still new here,and a beginner with this C++ program. im busy with an assignment with 2D arrays.
most of the questions i did get right and some only half and then one of them not at all hehe.
first how do i make a tabel with string names? and then the second question is how do i determine the smallest of all in a 2D array. here is my program below
#include <iostream>
using namespace std;

const int NUM_FRIENDS = 4;
const int NUM_MODULES = 3;

void getdata(int inputP[] [NUM_MODULES])
{ int friends;

for (int i = 0;i < NUM_FRIENDS; i++)
{ cout << "Enter" << NUM_MODULES << "hour interfals studied weekely on each module " << endl;

for (int j = 0;j < NUM_MODULES;j++)
{
cin >> inputP[i][j];
}
}
}

void showdata(const int inputP[] [NUM_MODULES])
{ string friends;

//headings
for (int j = 0;j < NUM_MODULES;j++)
{
cout <<'\t'<< "ABC111";
}
cout << endl;

//rows.
for (int i = 0;i < NUM_FRIENDS;i++)
{ cout << "Aggie : ";
// cout << "Bruce : ";
/// cout << "Cecil : ";
// cout << "Danita : ";

for (int j = 0;j < NUM_MODULES;j++)
cout << inputP[i][j] << '\t';
cout << endl;
}
}

int hoursLeast(const int inputP[] [NUM_MODULES])
{


}

void displayAverage (const int inputP[] [NUM_MODULES])
{ int total;
float average;

cout << "the average of each module is : " << endl;

for (int i = 0;i < NUM_MODULES;i++)
{ total = 0;
cout << "Module " << i+1 << " : ";

for (int j = 0;j < NUM_FRIENDS;j++)
total = total + inputP[i][j];
average = float(total) / NUM_FRIENDS;
cout << average << endl;
}
}

int main ()
{ int input[NUM_FRIENDS][NUM_MODULES];

getdata(input);
showdata(input);

hoursLeast(input);
cout << endl;
displayAverage(input);

return 0;
}

it should look like this :

ABC111 DEF222 GHI333
aggie 11 9 7
bruce 9 10 13
cecil 8 8 8
danita 12 8 9

where mine only show at the top row abc111, and the rows only aggie. when i try to change it so that all shows. its not displaying right..
im really stuck :(
Last edited on
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
#include <iostream>
using namespace std;
const int NUM_FRIENDS = 4;
const int NUM_MODULES = 3;

void getdata(int inputP[] [NUM_MODULES])
{ int friends;

for (int i = 0;i < NUM_FRIENDS; i++)
{ cout << "Enter" << NUM_MODULES << "hour interfals studied weekely on each module " << endl;

for (int j = 0;j < NUM_MODULES;j++)
{
cin >> inputP[i][j];
}
}
}

void showdata(const int inputP[] [NUM_MODULES])
{ string friends;
string name[4]={"Aggie","Bruce","Cecil","Danita"};

cout <<'\t'<< "ABC111"<<'\t'<< "DEF222"<<'\t'<< "GHI333";

cout << endl;

//rows.
for (int i = 0;i < NUM_FRIENDS;i++)
{ cout << name[i];

for (int j = 0;j < NUM_MODULES;j++)
cout <<'\t'<< inputP[i][j];
cout << endl;
}
}

int hoursLeast(const int inputP[] [NUM_MODULES])
{


}

void displayAverage (const int inputP[] [NUM_MODULES])
{ int total;
float average;

cout << "the average of each module is : " << endl;

for (int i = 0;i < NUM_MODULES;i++)
{ total = 0;
cout << "Module " << i+1 << " : ";

for (int j = 0;j < NUM_FRIENDS;j++)
total = total + inputP[i][j];
average = float(total) / NUM_FRIENDS;
cout << average << endl;
}
}

int main ()
{ int input[NUM_FRIENDS][NUM_MODULES];

getdata(input);
showdata(input);
hoursLeast(input);
cout << endl;
displayAverage(input);
return 0;
}


should fix the first part...
Last edited on
thank you ... now just to figure out the smallest hour :)
Topic archived. No new replies allowed.