Please make the following code short.

Asked 2 years ago, Updated 2 years ago, 36 views

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;
}

c

2022-09-30 19:23

1 Answers

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.

  • An array declaration of the student information structure is an array of pointers, and an actual data area is not secured.
    It is also used even though the area is not secured.(The build does not pass for the following reasons, but even if it can be built, it will terminate abnormally due to memory access violation)
  • Variables named 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?
    (Therefore, the build cannot be done properly.)
  • None of the three for loops has an exit condition specified.
  • Determination criteria for 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:

  • A maximum value (10) of the number of input data is defined as a constant macro.
  • An array of student information structures is declared as an array of structures with real areas, not pointers.
  • Prepare 13 variables including the following (int as well as double depending on the content of the assignment)
    • loop counters
    • Number of valid input data (initial value is 10)
    • Three for entering your school registration number, height, and weight
    • The highest/lowest value of height/weight and 8 corresponding school registration numbers
      Initialize each value with an unlikely number (student registration number and maximum value of -1, minimum value of 99999, etc.)
  • for loop:
    • for conditions for continuing/terminating (loop counters continue below 10; ends above 10)
    • Prompt input guideprintf
    • Reading input values using variables for entering school registration number, height, and weight as parametersscanf
    • Make a negative student number entry decision and update the number of valid input data to escape the loop
    • if it ends halfway.
    • Each input data is stored in an array of student information structures
    • Determine whether the highest/lowest value of height/weight is updated, and if so, replace the number with the school registration number
  • forIf 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.
  • loop counters
  • Number of valid input data (initial value is 10)
  • Three for entering your school registration number, height, and weight
  • The highest/lowest value of height/weight and 8 corresponding school registration numbers
    Initialize each value with an unlikely number (student registration number and maximum value of -1, minimum value of 99999, etc.)
  • for conditions for continuing/terminating (loop counters continue below 10; ends above 10)
  • Prompt input guideprintf
  • Reading input values using variables for entering school registration number, height, and weight as parametersscanf
  • Make a negative student number entry decision and update the number of valid input data to escape the loop
  • if it ends halfway.
  • Each input data is stored in an array of student information structures
  • Determine whether the highest/lowest value of height/weight is updated, and if so, replace the number with the school registration number


2022-09-30 19:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.