### Sample codes... :)

Pages: 12
hi! i want to make a program that will input 5 student names and will input 4 grades for each student then will output the student name with the average of his/her grade using 2d array... this is what i've come up with so far...
 12345678910111213141516171819202122232425 #include #include using namespace std; main() { string studentgrade[5][4]; string row, col; int r, c; for (r = 0; r < 5; r++) { cout << "enter name of student: " << r + 1 << endl; getline(cin,row); for (c = 0; c < 4; c++) { cout << "enter grade for student: " << r + 1 << endl; getline(cin,col); } cout<

it doesn't run... at all... i'm having a hard time making it... i'm new at this... please help! :(

this is the sample given to us by our instructor...

 123456789101112131415161718192021222324252627282930313233343536373839404142 #include using namespace std; main() { string name[5]; string term[4] = {"Prelim","Midterm","Semi-Final","Final"}; string grade1[4]; cout<<"Enter the name of the student:\n\n"; for(int i=0;i<5;i++) { cout<<"Enter the name of student "<>grade1[i]; } cout<<"\n\nGrade for student named "<

it's not really complete...
Last edited on
closed account (Dy7SLyTq)
first you need #include <string>
second make a function that computes the average
ok here's the thing... i'm really new with c++... my programming 1 used java... and i took a break from school for 2 years... didn't really practice any of my programming skills... i don't know a lot of the commands used in c++... so i'm really having a hard time...
can you see the 2 lines on top? the ones starting with ( #include ), those are called include directives.
the cout word is an object defined in the "iostream" header, so if you want to use cout, you shall "include" the iostream header to be compiled into your program.
the same is with string, if you want to use it, you should include a header called string like this:

#include <string>

each directive must be on it's own line.
the includes must be outside the body of anything.
ok... but how do i make a program that will input a student's name then give that particular name it's grade for prelim, midterm, semi-finals and final... 5 students...

and the output will show the student's name and his average grade... using a 2d array...
i'm done! had some help with my classmates...

 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 #include #include #include using namespace std; main() { string student[5]; double grades[5][4]; string term[4] = {"Prelim", "Midterm", "Semi-Final", "Final"}; int i, row, col; double ave[5]; for (i = 0; i < 5; i++) { cout << "Enter Name Of Student #" << i + 1 << ": "; getline(cin, student[i]); } for (row = 0; row < 5; row++) { cout << "Enter Grades for " << student[row] << ":" << endl << endl; for (col =0; col < 4; col++) { cout << term[col] << ": "; cin >> grades[row][col]; ave[row] = ave[row] + grades[row][col]; } cout << endl; ave[row] = ave[row]/4; } for (row = 0; row < 5; row++) { cout << "Name of Student #" << row + 1 << " " << student[row] << endl; for (col = 0; col < 4; col++) { cout << term[col] << ": " << grades[row][col] << endl; } cout << "\nWith an Average of " << ave[row] << endl << endl; } cout << endl; system("PAUSE"); return 0; }
updated version... :)

 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 #include #include #include using namespace std; main() { string student[5]; string term[4] = {"Prelim", "Midterm", "Semi-Final", "Final"}; int grades[5][4], i, row, col; double ave[5], aveterm[4]; for (i = 0; i < 5; i++) { cout << "Enter Name Of Student #" << i + 1 << ": "; getline(cin, student[i]); cout << endl; } for (row = 0; row < 5; row++) { cout << "Enter Grades for " << student[row] << ":" << endl << endl; for (col =0; col < 4; col++) { cout << term[col] << ": "; cin >> grades[row][col]; } cout << endl; } for (row = 0; row < 5; row++) { for (col =0; col < 4; col++) { ave[row] = ave[row] + grades[row][col]; } ave[row] = ave[row]/4; } for (col = 0; col < 4; col++) { for (row = 0; row < 5; row++) { aveterm[col] = aveterm[col] + grades[row][col]; } aveterm[col] = aveterm[col]/5; } cout << " Student # | Student Name | P || M || S || F || Average" << endl; cout << "-----------------------------------------------------------------------------" << endl; for (row = 0; row < 5; row++) { cout << " " << row + 1 << " | " << student[row] << "\t\t"; for (col = 0; col < 4; col++) { cout << "| " << grades[row][col] << " |"; } cout << "| " << ave[row] << " "; cout << endl; } cout << " | Average per Term\t\t"; for (col = 0; col < 4; col++) { cout << "| " << aveterm[col] << " |"; } cout << "\n-----------------------------------------------------------------------------" << endl; cout << endl; system("PAUSE"); return 0; }
Last edited on
 123456789101112131415161718192021222324252627282930313233343536373839404142434445 #include using namespace std; main() { int r,c,j=0,i=10,a[10][10]; for(r=0;r<10;r++) { for (c=0;c<10;c++) { a[r][c]=0; } } for(r=0;r<10;r++) { for (c=0;c<10;c++) { cout<0;c--) { cout<<" "; } for(c=r;c

 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 #include using namespace std; main() { int r,c,j=0,i=10,a[10][10]; for(r=0;r<10;r++) { for (c=0;c<10;c++) { a[r][c]=0; } } for(r=0;r<10;r++) { for (c=0;c<10;c++) { cout<i-2;c--) { cout<

 12345678910111213141516171819202122232425262728293031323334 #include using namespace std; main () { int r, c, a[10][10]; for (r=0;r<10;r++) { for(c=0;c<10;c++) { a[r][c]=0; } } for (r=0;r<5;r++) { for(c=0;c<=r;c++) { cout<0;r--) { for(c=0;c

 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 #include using namespace std; main() { int r,c,j=0,i=10,a[10][10]; for(r=0;r<10;r++) { for (c=0;c<10;c++) { a[r][c]=0; } } for(r=0;r<10;r++) { for (c=0;c<10;c++) { cout<4;r--) { for(c=i;c>0;c--) { cout<<" "; } for(c=0;c0;r--) { for(c=j-1;c>0;c--) { cout<<" "; } for(c=i+1;c>0;c--) { cout<

 1234567891011121314151617181920212223242526272829303132 #include #include using namespace std; main() { int r,c; string a[10][10]; for(r=0;r<10;r++) { for (c=0;c<10;c++) { a[r][c]="0"; } } for(r=0;r<10;r++) { for (c=0;c<10;c++) { if (r==c) cout<<"* "; else cout<
Last edited on
 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 #include using namespace std; main() { int r,c,j=0,i=10,l=0,m=10,a[10][10]; for (r = 0; r < 10; r++) { for (c = 0; c < 10; c++) { a[r][c] = 0; } } for (r = 0; r < 10; r++) { for (c = 0; c < 10; c++) { cout << a[r][c] << " "; } cout << endl; } cout << "\n\n"; for(r=j;r<10;r++) { for(c=j+1;c<10;c++) { cout<<" "; } for(c=r;c>i-2;c--) { cout<0;c--) { cout<<" "; } for(c=r;c
Last edited on
I would have thought to not try and use one 2d array because names would be a string and grades averages would be dealing with math.

these are not easily compatable. wouldn't it be easier to use structs and then put your structs into an array of structs, if needed?

something like...

 1234567 struct student { string first_name; string last_name; int grades[4]; int gradeAverage; }; 

this would be my approach.

Last edited on
 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 #include using namespace std; main() { int ***wee, l, w, d, i, j, k; cout<<"Enter side of wee\n"; cout<<"l: "; cin>>l; cout<<"\nw: "; cin>>w; cout<<"\nd: "; cin>>d; wee=new int** [l]; cout<>wee[i][j][k]; cout<
 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 #include using namespace std; int n; struct b { string m; string d; string y; }; struct p { string fn; string mn; string ln; string g; int a; double h; float w; b bd; }; main() { int i, j, h; cout<<"Enter number of students: "; cin>>n; p *s; s = new p [n]; for(i=0; i>s[i].fn; cout<<"Enter the middle name of "<>s[i].mn; cout<<"Enter the last name of "<>s[i].ln; cout<<"Enter the age of "<>s[i].a; cout<<"Enter the gender of "<>s[i].g; cout<<"Enter the birthday of "<>s[i].bd.m; cout<<"Day: "; cin>>s[i].bd.d; cout<<"Year: "; cin>>s[i].bd.y; cout<<"Enter the weight of "<>s[i].w; cout<<"Enter the height of "<>s[i].h; } for(j=n-1; j>0; j--) { for(h=0; hs[h+1].ln) { string t = s[h].fn; s[h].fn = s[h+1].fn; s[h+1].fn = t; string t2 = s[h].mn; s[h].mn = s[h+1].mn; s[h+1].mn = t2; string t3 = s[h].ln; s[h].ln = s[h+1].ln; s[h+1].ln = t3; int t4 = s[h].a; s[h].a = s[h+1].a; s[h+1].a = t4; string t5 = s[h].g; s[h].g = s[h+1].g; s[h+1].g = t5; string t6 = s[h].bd.m; s[h].bd.m = s[h+1].bd.m; s[h+1].bd.m = t6; string t7 = s[h].bd.d; s[h].bd.d = s[h+1].bd.d; s[h+1].bd.d = t7; string t8 = s[h].bd.y; s[h].bd.y = s[h+1].bd.y; s[h+1].bd.y = t8; float t9 = s[h].w; s[h].w = s[h+1].w; s[h+1].w = t9; double t10 = s[h].h; s[h].h = s[h+1].h; s[h+1].h = t10; } } } cout<<"\n\n"; for(i=0; i
Last edited on
 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 # include using namespace std; struct student { string fullName; int mathGrade; int physicsGrade; int historyGrade; int cookingGrade; }; int main() { student st[5]; cout << "Load students info" << endl << endl; for(int i=0;i<5;i++) { cout << "==============" << endl; cout << "Student nbr " << i+1 << endl; cout << "==============" << endl; cout << "Enter the whole name -> ";getline(cin,st[i].fullName); cout << "Enter math grade -> ";cin >> st[i].mathGrade; cout << "Enter physics grade -> ";cin >> st[i].physicsGrade; cout << "Enter history grade -> ";cin >> st[i].historyGrade; cout << "Enter cooking grade -> ";cin >> st[i].cookingGrade; cout << endl; cin.ignore(); } cout << endl; // if you want to show all of students info cout << "Display students info" << endl << endl; for(int i=0;i<5;i++) { cout << "==============" << endl; cout << "Student nbr " << i+1 << endl; cout << "==============" << endl; cout << "Enter the whole name -> " << st[i].fullName << endl; cout << "Enter math grade -> " << st[i].mathGrade << endl; cout << "Enter physics grade -> " << st[i].physicsGrade << endl; cout << "Enter history grade -> " << st[i].historyGrade << endl; cout << "Enter cooking grade -> " << st[i].cookingGrade << endl << endl; } return 0; }

I hope that helps
Last edited on
manga and techno1... thanks... it really did help... it made the program simpler and a lot easier to manipulate the data... :)
You're welcome dont forget to mark it as solved(It realy helps this community!)
if i "mark it as solved"... will i still be able to post here?...
closed account (Dy7SLyTq)
yeah
 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 #include using namespace std; struct s{ int x; int y; int add(int a, int b) { int r; r=a+b; return (r); }; int sub(int a, int b) { int r; r=a-b; return (r); }; double mul(double a, double b) { double r; r=a*b; return (r); }; float div(float a, float b) { float r; r=a/b; return (r); }; }; main() { s op; cout<<"enter value for x: "; cin>>op.x; cout<<"enter value for y: "; cin>>op.y; cout<<"\n\n"; cout<<"\nAddition: "<
Last edited on
 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 #include #include #include using namespace std; string en(string x) { for(int i=0;i
Last edited on
closed account (Dy7SLyTq)
omfg... we must kill all necromancers kel' thuzad style. ill grab a hammer
Pages: 12