problem with return and calling
Apr 2, 2014 at 5:12pm UTC
so what i want to do is design a program that will give me the entered grades by order , the thing is .i don't know how and i have to use a struct and a function and pointers
my idea was sort of like this :
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
#include <iostream>
using namespace std;
struct info{
char name[20];
int grade;
};
int order(struct info a[]){
int x;
x=a->grade;
for (int i=0;i<15;i++){
if (x < a->grade){ x = a->grade;}
}
return x;
};
int main(){
struct info student[15];
for (int i=0;i<15;i++){
cout<<"/nenter the name: " ;
cin>>student[i].name;
cout<<"/nEnter the grade" ;
cin>>student[i].grade;
}
int *p;
p= order(student);
it's completely wrong and sloppy i know . but i really got stuck on what to do and how
any advice?
Last edited on Apr 2, 2014 at 8:23pm UTC
Apr 2, 2014 at 8:23pm UTC
I kinnda fixed most of it the only problem now is in the return and calling of the function
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;
struct things{
char name[20];
int grade;
};
things order(struct things a[]){
int *x,i,j,k;
x=&a->grade;
for (i = 0; i < 5; ++i)
{
for (j = i + 1; j < 5; ++j)
{
if (a[i].grade < a[j].grade)
{
k = a[i].grade;
a[i].grade = a[j].grade;
a[j].grade = k;
}
}
}
return a;
};
int main(){
struct things student[5];
for (int i=0;i<5;i++){
cout<<"enter the name: " ;
cin>>student[i].name;
cout<<"Enter the grade" ;
cin>>student[i].grade;
}
struct things *p;
p= order(student);
cout<<*p<<*(p+1);
}
Apr 2, 2014 at 8:47pm UTC
Search "passing arrays as function parameters". You will find your answer.
Topic archived. No new replies allowed.