I'm trying to sort out the grades, but subscripts will not go to their right spot. I've tried to remove int in each for loop, except the first one, but there was an error saying
‘sub’ was not declared in this scope
And the output looks like this
Enter 15 exam grades: 90 80 70 60 50 91 81 71 61 51 92 82 72 62 52
Students who earned an A: 3 5 9 11 14
Students who earned a B:
Students who earned a C:
Students who earned a D:
Students who earned a F: 1 2 4 6 7 8 10 12 13 15
I assume there was something wrong with the compiler, but I'm sure.
Here is my code
#include <iostream>
using namespace std;
int main()
{
/* Declaring variables */
const int SIZE = 15;
int grades[SIZE];
/* Message Prompt */
cout << "Enter " << SIZE << " exam grades: ";
for (int sub = 0; sub < SIZE; sub++)
cin >> grades[SIZE];
/* Results Display for A */
cout << "Students who earned an A: ";
for (int sub = 0; sub < SIZE; sub++)
{
if (grades[sub] >= 90)
{
cout << sub + 1 << " ";
}
}
cout << endl;
/* Results Display for B */
cout << "Students who earned a B: ";
for (int sub = 0; sub < SIZE; sub++)
{
if ((grades[sub] <= 89) && (grades[sub] >= 80))
{
cout << sub + 1 << " ";
}
}
cout << endl;
/* Results Display for C */
cout << "Students who earned a C: ";
for (int sub = 0; sub < SIZE; sub++)
{
if ((grades[sub] <= 79) && (grades[sub] >= 70))
{
cout << sub + 1 << " ";
}
}
cout << endl;
/* Results Display for D */
cout << "Students who earned a D: ";
for (int sub = 0; sub < SIZE; sub++)
{
if ((grades[sub] <= 69) && (grades[sub] >= 60))
{
cout << sub + 1 << " ";
}
}
cout << endl;
/* Results Display for F */
cout << "Students who earned a F: ";
for (int sub = 0; sub < SIZE; sub++)
{
if (grades[sub] <= 59)
{
cout << sub + 1 << " ";
}
}
cout << endl;
return 0;
}
This line is wrong:
for (int sub = 0; sub < SIZE; sub++) cin >> grades[SIZE];
It should be grades[sub] instead.
Related
I can't figure out why I can't calculate the average. The output of the average is same as the other look the details in the picture below. My teacher told me to return the AVG to 0, but I don't know where to put it.
#include <iostream>
using namespace std;
const int ROW = 3;
const int COL = 3;
const int D = 3;
void display(int nums[ROW][COL]);
void show(int ID[D]);
int main()
{
int nums[ROW][COL];
int ID[D];
double AVG = 0;
for (int i = 1; i <= ROW; i++) {
cout << "Enter student ID[" << i << "]: ";
cin >> ID[i];
for (int j = 0; j < COL; j++) {
cout << "Enter student[" << i << "] Grades [" << j + 1 << "]:";
cin >> nums[i][j];
AVG = AVG + nums[i][j];
}
AVG = AVG / 3;
}
for (int i = 1; i <= ROW; i++) {
cout << "Student ID: " << ID[i] << endl;
for (int j = 0; j < COL; j++) {
cout << "Score: " << nums[i][j] << endl;
}
cout << "Average: " << AVG << endl;
}
return 0;
}
You Have some problem regarding this issues:
you take AVG as double but other variable as integer.so there is a datatype error.
2.you have problem doing array indexing.
Here Is the Solution. Here I declare AVG as an integer type array so that it remains same as your code.
#include
using namespace std;
const int ROW = 3;
const int COL = 3;
const int D = 3;
void display(int nums[ROW][COL]);
void show(int ID[D]);
int main ()
{
int nums[ROW][COL];
int ID[D];
int AVG[ROW];
for(int i=1; i<=ROW; i++)
{
cout << "Enter student ID[" << i <<"]: ";
cin>>ID[i];
AVG[i-1]=0;
for(int j=0; j<COL; j++)
{
cout<<"Enter student[" << i << "] Grades [" <<j+1<<"]:" ;
cin>>nums[i-1][j];
AVG[i-1] = AVG[i-1] + nums[i-1][j];
}
AVG[i-1] = AVG[i-1] / 3;
}
for(int i=1; i<=ROW; i++)
{
cout <<"Student ID: " << ID[i] << endl;
for(int j=0; j<COL; j++)
{
cout<<"Score: "<<nums[i-1][j]<<endl;
}
cout << "Average: " << AVG[i-1]<< endl;
}
return 0;
}
Currently you are calculating the average as follows;
First iteration:
(90 + 90 + 90) / 3 = 90
Second iteration:
(90 + 80 + 80 + 80)/3 = 110
Third iteration:
(110 + 70 + 70 + 70)/3 = 106
If you want to get the average of all grades, do this;
double AVG = 0;
double avg_tmp;
for (int i = 1; i <= ROW; i++) {
avg_tmp = 0;
cout << "Enter student ID[" << i << "]: ";
cin >> ID[i];
for (int j = 0; j < COL; j++) {
cout << "Enter student[" << i << "] Grades [" << j + 1 << "]:";
cin >> nums[i][j];
avg_tmp += nums[i][j];
}
AVG += avg_tmp / 3;
}
Your biggest issue is you invoke Undefined Behavior with your loop limits for for (int i = 1; i <= ROW; i++). In C/C++ all array indexes are Zero-Based with valid indexes of 0 <= i < n. So your i loop must be for (int i = 0; i < ROW; i++).
You do NOT need to compute the sum or average in your first set of loops. The first set of nested loops are you Input Loops where you are simply filling the elements of ID and nums. You only need AVG in your second set of nested loops (e.g. your Output Loops) Avoid mixing input and output logic.
Only declare and initialize your variables in the scope where they are needed. This will fix what your professor is talking above. If you only declare and initialize AVG within the first output-loop where the average will be computed, it will automatically be reset to zero for each student.
Putting it altogether, you could do:
#include <iostream>
const int ROW = 3; /* good job on declaring constants */
const int COL = 3;
const int D = 3;
int main()
{
int nums[ROW][COL] = {{0}}; /* initialize all plain-old arrays */
int ID[D] = {0};
for (int i = 0; i < ROW; i++) {
std::cout << "\nEnter student ID[" << i + 1 << "]: ";
/* you must validate EVERY user-input */
if (!(std::cin >> ID[i])) {
std::cerr << "error: invalid integer input.\n";
return 1;
}
for (int j = 0; j < COL; j++) {
if (!j)
std::cout.put('\n');
std::cout << "Enter student[" << i+1 << "] Grades [" << j+1 << "]: ";
if (!(std::cin >> nums[i][j])) { /* ditto */
std::cout << "error: invalid integer input.\n";
return 1;
}
}
}
for (int i = 0; i < ROW; i++) {
double AVG = 0.; /* declare / initialize in scope needed */
std::cout << "\nStudent ID: " << ID[i] << '\n';
for (int j = 0; j < COL; j++) {
std::cout << " Score: " << nums[i][j] << '\n';
AVG += nums[i][j]; /* sum each score */
}
std::cout << "Average: " << AVG / COL << '\n';
}
}
Example Use/Output
$ ./bin/studentavg
Enter student ID[1]: 1
Enter student[1] Grades [1]: 90
Enter student[1] Grades [2]: 89
Enter student[1] Grades [3]: 91
Enter student ID[2]: 2
Enter student[2] Grades [1]: 79
Enter student[2] Grades [2]: 81
Enter student[2] Grades [3]: 80
Enter student ID[3]: 3
Enter student[3] Grades [1]: 71
Enter student[3] Grades [2]: 70
Enter student[3] Grades [3]: 69
Student ID: 1
Score: 90
Score: 89
Score: 91
Average: 90
Student ID: 2
Score: 79
Score: 81
Score: 80
Average: 80
Student ID: 3
Score: 71
Score: 70
Score: 69
Average: 70
Look things over and let me know if you have further questions.
package test;
import java.util.*;
public class DoubleArray {
public static void main(String[] args) {
String[][] arr = {{"Ram", "100"}, {"Mohan", "80"}, {"Ram", "70"}, {"Mohan", "60"}, {"Chitra", "80"}, {"Ram", "88"}};
HashMap<String, Integer> pf = new HashMap<String, Integer>();
List<String> as = new ArrayList<>();
for (int row = 0; row < arr.length; row++) {
if (pf.containsKey(arr[row][0])) {
as.add(arr[row][0]);
pf.put(arr[row][0], ((pf.get(arr[row][0]) + Integer.parseInt(arr[row][1]))));
} else {
as.add(arr[row][0]);
pf.put(arr[row][0], Integer.parseInt(arr[row][1]));
}
}
int value = 0;
for (Map.Entry<String, Integer> entry : pf.entrySet()) {
pf.put(entry.getKey(), entry.getValue() / Collections.frequency(as, entry.getKey()));
value = Collections.max(pf.values());
if (entry.getValue() == value) {
System.out.println(entry.getKey() + "," + entry.getValue());
}
}
}
}
I have a homework question. I need help displaying my results in this format:
Price1 Price2 Price3
Price4 Price5 Price6
Price7 Price8 Price9
How can I display the results in the desired format in a cout statement? Here's my code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
// use a constant Declaration
const int SIZE = 9;
// use a variable Declaration
float prices[SIZE];
cout << fixed << setprecision(2);
// prompt user to enter all 9 values :
for(int i = 0; i < SIZE; i++)
{
cout << "Enter the value: " << i + 1 << "-> ";
cin >> prices[i];
}
cout << "\n----------------------------------------------------\n";
// Display all values in the array
for(int i = 0; i < SIZE; i++)
{
cout << "value Entered " << i + 1 << "\t\t " << right << setw(7) << prices[i] << endl;
}
}
This is the output when running the code:
Enter the value: 1-> 21
Enter the value: 2-> 34
Enter the value: 3-> 54
Enter the value: 4-> 12
Enter the value: 5-> 65
Enter the value: 6-> 34
Enter the value: 7-> 76
Enter the value: 8-> 88
Enter the value: 9-> 90
----------------------------------------------------
value Entered 1 21.00
value Entered 2 34.00
value Entered 3 54.00
value Entered 4 12.00
value Entered 5 65.00
value Entered 6 34.00
value Entered 7 76.00
value Entered 8 88.00
value Entered 9 90.00
--------------------------------
Process exited after 16.86 seconds with return value 0
Press any key to continue . . .
Here try this
#include <iostream>
using namespace std;
int main()
{
// use a constant Declaration
const int SIZE = 9;
// use a variable Declaration
float prices[SIZE];
// prompt user to enter all 10 values :
for (int i = 0; i < SIZE; i++)
{
cout << "Enter the value: " << i + 1 << "-> ";
cin >> prices[i];
}
cout << "\n----------------------------------------------------\n";
// Display all values in the array
int index = 0;
while (index < SIZE)
{
for (int ctr = 0; ctr < 3; ++ctr)
{
cout << "values Entered " << index + 1 << " " << prices[index] << "\t\t";
++index;
}
cout << endl;
}
}
This is a problem given to me by my computer mentor and I am trying to explain the problem in simple words(I must remind you that my mentor has only taught me loops array structure. I cannot use class for this problem)
Take two matrix
Print the name of n students in one matrix
Print the id, marks in 5 subject of the students in another 2d
matrix (n*6)
Find the student who got the highest in the total of 5 subject and
print the student name with marks of each subject and his total
marks.(i think we have to us a structure)
For example: The highest marks is secured by :
Name;
id no;
marks in sub 1;
marks in sub 2;
marks in sub 3;
marks in sub 4;
marks in sub 5;
total;
Then print the id and the name of the student who got highest in
each subject
For example:
Subject 1 ID name
Subject 2 ID name .....
I have being able to solve it fully till point (3). but unable to print the person who got the highest in each subject ; and I am facing problem to print the person who got highest in each subject as I have to use many variables(I'm not using structure)
What I have tried so far:
#include <iostream>
using namespace std;
int main()
{
//making 1st array to print the names of the students
char x[500][1000];
int num, i;
cout << "Enter the number of student's data you want to input" << endl;
cin >> num;
cout << endl;
cout << "NAME LIST" << endl;
cout << endl;
for(i = 0; i < num + 1; i++)
{
cin.getline(x[i], 1000);
}
//making a 2nd array to print the id, marks in 5 subjects of the students
int y[num][6];
int a, b;
cout << endl;
cout << endl;
cout << "DETAILS OF THE STUDENT" << endl;
cout << endl;
for(int a = 0; a < num; a++)
{
cout << "ID no of student " << a + 1 << ":";
cin >> y[a][0];
cout << "Marks in subject 1:";
cin >> y[a][1];
cout << "Marks in subject 2:";
cin >> y[a][2];
cout << "Marks in subject 3:";
cin >> y[a][3];
cout << "Marks in subject 4:";
cin >> y[a][4];
cout << "Marks in subject 5:";
cin >> y[a][5];
cout << endl;
}
cout << endl;
cout << "The data you inputed:";
cout << endl;
for(a = 0; a < num; a++)
{
for(b = 0; b < 6; b++)
{
cout << y[a][b] << " ";
}
cout << endl;
}
cout << endl;
cout << endl;
//finding the member who got the highest marks
int s = 0;
int largestSum = 0;
for(a = 0; a < num; i++)
{
for(b = 0; b<6; b++)
{
s += y[a][b];
}
// check to see if we have computed a new larger sum and save it if we have.
if(s > largestSum)
{
largestSum = s;
}
}
cout << "largest sum: " << largestSum << endl;
return 0;
}
int s = 0; <- s needs to be reset to zero in each loop
int largestSum = 0;
for(a = 0; a<num; i++) <- loop doesn't break
{
for(b = 0; b<6; b++) <- b should start at 1
{
s += y[a][b];
}
if(s > largestSum)
{
largestSum = s;
}
}
i is incremented while a remains constant and the loop goes forever.
s is not reset to zero, so you sum all the values.
The first index at b = 0 is supposed to be student id, it should be skipped. Try instead:
int largestSum = 0;
for(a = 0; a < num; a++)
{
int s = 0;
for(b = 1; b < 6; b++)
s += y[a][b];
if(s > largestSum)
largestSum = s;
}
Here's something to go off of.
Note that I only made one matrix and put the Student objects in it.
#include <ctime>
#include <iostream>
#include <string>
#include <vector>
const std::vector<std::vector<std::string>> names = {
{"Ralph", "Gerald", "Henry", "Jessica", "Bob"},
{"Tara", "Tami", "Mike", "Loretta", "Jean"},
{"Jesse", "John", "Carl", "Josh", "Abby"},
{"Carson", "Don", "George", "Hillary", "David"},
{"Micah", "Charlie", "Maximus", "Leonidas", "Xerxes"}
};
int main()
{
time_t seconds;
time(&seconds);
srand((unsigned int)seconds);
struct Subjects
{
unsigned int subject[5] = {};
};
struct Student
{
Subjects sub;
std::string name;
int id;
Student() : name("nobody"), id(-1) {};
Student(const std::string& name, const unsigned int& id) : name(name), id(id) {}
};
Student stud_matrix[5][5] = {};
int count = 0;
for (size_t i = 0; i != 5; ++i)
{
for (size_t j = 0; j != 5; ++j)
{
stud_matrix[i][j].name = names[i][j];
stud_matrix[i][j].id = count;
++count;
}
}
std::cout << "All student names:\n";
for (size_t i = 0; i != 5; ++i)
{
for (size_t j = 0; j != 5; ++j)
{
std::cout << stud_matrix[i][j].name << ' ';
}
std::cout << std::endl;
}
std::cout << std::endl;
std::cout << "All student IDs:\n";
for (size_t i = 0; i != 5; ++i)
{
for (size_t j = 0; j != 5; ++j)
{
std::cout << stud_matrix[i][j].id << ' ';
}
std::cout << std::endl;
}
std::cout << std::endl;
for (size_t i = 0; i != 5; ++i)
{
for (size_t j = 0; j != 5; ++j)
{
stud_matrix[i][j].sub.subject[0] = (rand() % 100 + 1);
stud_matrix[i][j].sub.subject[1] = (rand() % 100 + 1);
stud_matrix[i][j].sub.subject[2] = (rand() % 100 + 1);
stud_matrix[i][j].sub.subject[3] = (rand() % 100 + 1);
stud_matrix[i][j].sub.subject[4] = (rand() % 100 + 1);
}
}
Student best_student;
unsigned int highest_grade = {};
for (size_t i = 0; i != 5; ++i)
{
for (size_t j = 0; j != 5; ++j)
{
if (stud_matrix[i][j].sub.subject[0] > highest_grade)
{
highest_grade = stud_matrix[i][j].sub.subject[0];
best_student = stud_matrix[i][j];
}
if (stud_matrix[i][j].sub.subject[1] > highest_grade)
{
highest_grade = stud_matrix[i][j].sub.subject[1];
best_student = stud_matrix[i][j];
}
if (stud_matrix[i][j].sub.subject[2] > highest_grade)
{
highest_grade = stud_matrix[i][j].sub.subject[2];
best_student = stud_matrix[i][j];
}
if (stud_matrix[i][j].sub.subject[3] > highest_grade)
{
highest_grade = stud_matrix[i][j].sub.subject[3];
best_student = stud_matrix[i][j];
}
if (stud_matrix[i][j].sub.subject[4] > highest_grade)
{
highest_grade = stud_matrix[i][j].sub.subject[4];
best_student = stud_matrix[i][j];
}
}
}
std::cout << "The highest scoring student in any one subject is: " << best_student.name << std::endl;
std::cout << "ID: " << best_student.id << std::endl;
std::cout << "Marks in sub1: " << best_student.sub.subject[0] << std::endl;
std::cout << "Marks in sub2: " << best_student.sub.subject[1] << std::endl;
std::cout << "Marks in sub3: " << best_student.sub.subject[2] << std::endl;
std::cout << "Marks in sub4: " << best_student.sub.subject[3] << std::endl;
std::cout << "Marks in sub5: " << best_student.sub.subject[4] << std::endl;
std::cout << "Total: " << best_student.sub.subject[0] + best_student.sub.subject[1] + best_student.sub.subject[2] +
best_student.sub.subject[3] + best_student.sub.subject[4] << std::endl;
std::cout << std::endl;
Student stud_sub1;
Student stud_sub2;
Student stud_sub3;
Student stud_sub4;
Student stud_sub5;
unsigned int high_sub1 = {};
unsigned int high_sub2 = {};
unsigned int high_sub3 = {};
unsigned int high_sub4 = {};
unsigned int high_sub5 = {};
for (size_t i = 0; i != 5; ++i)
{
for (size_t j = 0; j != 5; ++j)
{
if (stud_matrix[i][j].sub.subject[0] > high_sub1)
{
high_sub1 = stud_matrix[i][j].sub.subject[0];
stud_sub1 = stud_matrix[i][j];
}
if (stud_matrix[i][j].sub.subject[1] > high_sub2)
{
high_sub2 = stud_matrix[i][j].sub.subject[1];
stud_sub2 = stud_matrix[i][j];
}
if (stud_matrix[i][j].sub.subject[2] > high_sub3)
{
high_sub3 = stud_matrix[i][j].sub.subject[2];
stud_sub3 = stud_matrix[i][j];
}
if (stud_matrix[i][j].sub.subject[3] > high_sub4)
{
high_sub4 = stud_matrix[i][j].sub.subject[3];
stud_sub4 = stud_matrix[i][j];
}
if (stud_matrix[i][j].sub.subject[4] > high_sub5)
{
high_sub5 = stud_matrix[i][j].sub.subject[4];
stud_sub5 = stud_matrix[i][j];
}
}
}
std::cout << "Best of subject:\n";
std::cout << "Subject 1:\nID: " << stud_sub1.id << ' ' << stud_sub1.name << " Score: " << high_sub1 << std::endl;
std::cout << "Subject 2:\nID: " << stud_sub2.id << ' ' << stud_sub2.name << " Score: " << high_sub2 << std::endl;
std::cout << "Subject 3:\nID: " << stud_sub3.id << ' ' << stud_sub3.name << " Score: " << high_sub3 << std::endl;
std::cout << "Subject 4:\nID: " << stud_sub4.id << ' ' << stud_sub4.name << " Score: " << high_sub4 << std::endl;
std::cout << "Subject 5:\nID: " << stud_sub5.id << ' ' << stud_sub5.name << " Score: " << high_sub5 << std::endl;
system("pause");
return 0;
}
Sample Output:
All student names:
Ralph Gerald Henry Jessica Bob
Tara Tami Mike Loretta Jean
Jesse John Carl Josh Abby
Carson Don George Hillary David
Micah Charlie Maximus Leonidas Xerxes
All student IDs:
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
20 21 22 23 24
The highest scoring student in any one subject is: Bob
ID: 4
Marks in sub1: 77
Marks in sub2: 67
Marks in sub3: 7
Marks in sub4: 99
Marks in sub5: 66
Total: 316
Best of subject:
Subject 1:
ID: 23 Leonidas Score: 95
Subject 2:
ID: 18 Hillary Score: 92
Subject 3:
ID: 19 David Score: 98
Subject 4:
ID: 4 Bob Score: 99
Subject 5:
ID: 22 Maximus Score: 98
I trying to create a program that receives and stores information about in a dynamic array of structures. And the program should sort and display the teams above average, average and below avaerage. This is my code so far. . So what I'm doing is I receive the user input the check the input before storing it in dynamic structure array. Then finally I display all the information stored in the struct. Here's the output that i'm currently getting and I'm not sure why i'm getting this negative numbers.any ideas why?
Thanks
How many teams do you want to store? 2
Enter the name of the team 1:Vikings
Enter the team 1 percentage: 90
Enter the name of the team 2:PackersGreen Bay Packers
Enter the team 2 percentage: 80
Above Average :
Vikings 90%
PackersGreen Bay Packers 80%
Average :
5.00136e-317%
None
Below Average :
None
9.25737e-306%
Here's my code.
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
struct aboveAverage
{
string name9;
double percentage1;
};
struct average
{
string name10;
double percentage2;
};
struct belowAverage
{
string name11;
double percentage3;
};
int numOfteams;
double userInput;
cout << "How many teams do you want to store? ";
cin >> numOfteams;
cin.get();
aboveAverage * arrayOfAboveAverage = new aboveAverage[numOfteams];
average * arrayOfAverage = new average[numOfteams];
belowAverage * arrayOfbelowAverage = new belowAverage[numOfteams];
for (int i = 0; i < numOfteams; i++)
{
start:
int x = i + 1;
string name5;
cout << "Enter the name of the team " << x << ":";
getline(cin, name5);
cout << "Enter the team " << x << " percentage: ";
cin >> userInput;
cin.get();
if (userInput >= 66 && userInput <= 100)
{
arrayOfAboveAverage[i].percentage1 = userInput;
arrayOfAboveAverage[i].name9 = name5;
}
else if (userInput <= 66 && userInput >= 33)
{
arrayOfAverage[i].name10 = name5;
arrayOfAverage[i].percentage2 = userInput;
}
else if (userInput <= 33 && userInput >= 0)
{
arrayOfbelowAverage[i].name11 = name5;
arrayOfbelowAverage[i].percentage3 = userInput;
}
else
{
cout << "Percent cannot be greater than 100" << endl;
goto start;
}
}
cout << "Above Average :" << endl;
for (int j = 0; j < numOfteams; j++)
{
if (arrayOfAboveAverage[j].percentage1 != NULL ||
arrayOfAboveAverage[j].name9 != "")
{
cout << arrayOfAboveAverage[j].name9 <<" ";
cout << arrayOfAboveAverage[j].percentage1 <<"%"<< endl;
}
else
{
cout << "None" << endl;
}
}
cout << "Average :" << endl;
for (int j = 0; j < numOfteams; j++)
{
if (arrayOfAverage[j].percentage2 > 0 ||
arrayOfAverage[j].name10 != "")
{
cout << arrayOfAverage[j].name10 <<" ";
cout << arrayOfAverage[j].percentage2 <<"%"<<endl;
}
else
{
cout << "None" << endl;
}
}
cout << "Below Average : "<< endl;
for (int k = 0; k < numOfteams; k++)
{
if (arrayOfbelowAverage[k].percentage3 > 0 ||
arrayOfbelowAverage[k].name11 != "")
{
cout << arrayOfbelowAverage[k].name11 << " ";
cout << arrayOfbelowAverage[k].percentage3 <<"%"<< endl;
}
else
{
cout << "None" << endl;
}
}
delete[] arrayOfAboveAverage;
delete[] arrayOfAverage;
delete[] arrayOfbelowAverage;
return 0;
}
The problem is in the following test
if (arrayOfAverage[j].percentage2 > 0 ||
arrayOfAverage[j].name10 != "")
When arrayOfAverage is uninitialized (as in your case) name10 is initialized with the default value for a std::string (the empty string) but the value for percentage2 (a double) is undefined.
You test both values with "or", not "and", so if percentage2 is initialized with a positive value (by example: 5.00136e-317) you enter in the true case.
Suggestion: when there is a useful value, the name10 value isn't empty, so ignore percentage2 and modify the test as follows
if ( ! arrayOfAverage[j].name10.empty() )
Same problem with the preceding
if (arrayOfAboveAverage[j].percentage1 != NULL ||
arrayOfAboveAverage[j].name9 != "")
and the following test
if (arrayOfbelowAverage[k].percentage3 > 0 ||
arrayOfbelowAverage[k].name11 != "")
My suggestion is to modify they as follows
if ( ! arrayOfAboveAverage[j].name9.empty() )
// ...
if ( ! arrayOfBelowAverage[j].name11.empty() )
I have a problem with getting the min,max, and average of an elements of 2D array.
I have a 2D array which contains Students and Grades.
I am generating the grades with rand. For example When I enter 2,2 it prints me out
Courses : 01 02 Average Min Max
ID
01 8 50 29
02 74 59 29
, My average function takes the first ones average and doesnt take the others average.
Here is my code ;
int A[30][30];
int findAverage(int noOfStudents ,int noOfGrades ){
float sum,average;
for (int i = 0 ; i < noOfGrades ; i++) {
for (int j = 0; j<noOfStudents; j++) {
sum += A[i][j];
}
average = sum / noOfGrades;
// cout << " " << format(average);
sum = 0;
return format(average);
}
and here how I use it
int main() {
int noOfCourses , noOfStudents;
cin >> noOfCourses >> noOfStudents;
cout << "Courses : " ;
for (int i = 0; i < noOfCourses; i++) {
if (i+1 >= 10) {
cout << i+1 << " ";
}else{
cout <<"0" << i+1 << " ";
}
}
cout << "Average Min Max";
for(int i=0; i<noOfStudents; i++) { //This loops on the rows.
for(int j=0; j<noOfCourses; j++) { //This loops on the columns
A[i][j] = genGrade();
}
}
cout << "\n ID " << endl;
for(int i=0; i<noOfStudents; i++) { //This loops on the rows.
if (i+1 >= 10) {
cout <<" " << i+1 << " ";
}else{
cout <<" 0" << i+1 << " ";
}
//cout <<" 0" << i+1 << " ";
for(int j=0; j<noOfCourses; j++) { //This loops on the columns
if (A[i][j] >= 10 && A[i][j] <=99) {
cout <<" " << A[i][j] << " ";
}
if(A[i][j] < 10) {
cout <<" " << A[i][j] << " ";
}
if (A[i][j] == 100) {
cout << A[i][j] << " ";
}
}
cout <<" "<<findAverage(noOfStudents,noOfCourses);
cout << endl;
}
}
What am I doing wrong ? Also How could I get the min,max of the per array?
I would strong recommend using containers for this task. For example you could do the following
typedef std::vector<float> grades;
std::vector<grades> student_grades;
//populate
for(const grades& gr : student_grades) {
float min, max, avg;
std::tie(min, max)=std::minmax(gr.begin(), gr.end());
avg=std::accumulate(gr.begin(), gr.end(), 0.0) / gr.size());
std::cout << "Min" << min << " Max: " << max << " Avg: " << avg << std::endl;
}
http://en.cppreference.com/w/cpp/algorithm/minmax
http://en.cppreference.com/w/cpp/algorithm/accumulate
http://en.cppreference.com/w/cpp/utility/tuple/tie
For starters, you are returning from inside your loop:
for (int i = 0 ; i < noOfGrades ; i++) {
for (int j = 0; j<noOfStudents; j++) {
...
}
...
return ...;
}
Can you see how the outer loop will only ever execute once?
The problem is that in your findAverage function you have a return statement within the loop that is looping through the rows. If you want a very simple fix add another parameter to the findAverage function that indicates which row to calculate the average for.
int findAverage(int course ,int noOfGrades ){
float sum,average;
for (int j = 0; j<noOfStudents; j++) {
sum += A[course][j];
}
average = sum / noOfGrades;
return format(average);
}
and call it like this
cout <<" "<<findAverage(i,noOfCourses);