I've only been learning c for about a month.
The task is to create a program that defines structure student_data with school registration number, height, and weight as members, enters student information, and displays the highest and lowest values of height and weight along with school registration number.In addition, the maximum number of students is 10, and when a negative number is entered as the school registration number, the input is terminated.Give me some tips on the assignment.
The code below is a code in the middle of writing, but it seems that it is long and dirty, and the structure is used incorrectly.
I've just learned the pointer and structure, and I don't know what's wrong, so I think it's hard to read, but I'd appreciate it if you could teach me.
#include<stdio.h>
structure student_data{
int num, h, w;
};
int main() {
structure student_data*p[10];
inti, a = 0, b = 0, c = 0, d = 0;
printf("Input student number, height, weight:");
for(i=0;;i++){
scanf("%d%d%d", p[i]->num, p[i]->h, p[i]->w);
if(p[i]->num<0){
break;
} else if(i>=1){
if(p[i]->h-a->h>0){
p[i]->a;
} else {
p[i]->a;
}
}
}
for(i=0;;i++){
if(i>=1){
if(p[i]->h-c->h<0){
p[i]->c;
} else {
p[i]->c;
}
}
}
for(i=0;;i++){
if(i>=1){
if((p[i]->h)-(b->h)<0){
p[i]->b;
} else {
p[i]->b;
}
}
}
for(i=0;;i++){
if(i>=1){
if(p[i]->w-d->w<0){
p[i]->d;
}
} else {
p[i]->d;
}
}
printf("Tallest student number is %d(%d)\n", a.num, a.h);
return 0;
}
The only way to make it short is to have one for
loop.
In addition, there is no need to use structures for the content of the questions displayed in the text of the question.
You hardly need to use the pointer in a visible way.
By the way, the source of the question is harsh for the following reasons mentioned in the comment, but almost everything is not worth considering.
a
, b
, c
, d
of int
whose contents are unknown are declared, and programmed to be inconsistent by pointers to structures or membership of structures?for
loops has an exit condition specified.if
statements in each for
loop, and both true and false actions are not meaningful.It's a challenge, so it's better not to present the actual program source (but it's almost as good as presenting it).But), try programming based on the following:
int
as well as double
depending on the content of the assignment)
for
loop:
for
conditions for continuing/terminating (loop counters continue below 10; ends above 10)printf
scanf
for
If you exit the loop and have one or more valid data, display the highest/lowest height/weight values of the assignment and the corresponding school registration number.for
conditions for continuing/terminating (loop counters continue below 10; ends above 10)printf
scanf
© 2024 OneMinuteCode. All rights reserved.