Solve C Program Not Printing Homework

  • Thread starter Thread starter Mrencko
  • Start date Start date
  • Tags Tags
    Printing Program
Click For Summary

Discussion Overview

The discussion revolves around a C programming homework assignment that requires calculating the percentage of students who passed and failed based on their grades. Participants are addressing issues with the code implementation, particularly focusing on the correct counting of failed students and the handling of array indices.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant notes that the program does not work for an arbitrary number of students (N).
  • Another participant points out that the code does not display the number of failed students, despite calculating the percentage of failures.
  • Concerns are raised about an out-of-bounds error in the array indexing when checking for failed students.
  • A suggestion is made to combine the loops for counting passed and failed students to streamline the code.
  • One participant claims they have no problem running the code, indicating potential differences in execution or environment.
  • Fixes to the code are noted, including changing the loop bounds and conditions for counting failures.

Areas of Agreement / Disagreement

Participants express differing views on the functionality of the code, with some identifying specific errors and others asserting that the code runs correctly. There is no consensus on whether the original code is fundamentally flawed or if the issues are minor.

Contextual Notes

The discussion highlights limitations related to array indexing and the handling of student counts, which may affect the program's output. The scope of the problem is also limited to a fixed number of students (5) in the current implementation.

Mrencko
Messages
109
Reaction score
0

Homework Statement


one teacher wants to know the % of fail and aproved of the class of "N" number of students, the minimun qualification to aprove is "7", write a program that reads the students qualifications, and then the program says the number of aproved and failed students and the % of failed and aproved students.

Homework Equations


the code is in spanish my natal languaje but i am puting the translations of the code..
alumnos=students
reprobados=failed
aprobados=aproved
calification=qualification or the average sum of examns
indice=% of failed and aproved students

The Attempt at a Solution


its almost done but the program don't show the "how much students failed the curse"

here is my code
C:
#include<stdio.h>
int main(){
int x,a[5],ia,ir,repro=0,apro=0;
for(x=0;x<5;x++){
     printf("ingrese la calificacion del alumno %d:\n",x+1);
     scanf("%d",&a[x]);
}
for(x=0;x<5;x++){
     if(a[x]>7){
     apro++;}
}
for(x=6;x>0;x--){
     if(a[x]<7){
     repro--;}
}
printf("\n\nLos alumnos aprobados son %d\n",apro);
printf("\n\nLos alumnos reprobados son %d\n",repro);
ia=(2*apro)*10;
printf("\n\nEl indice de alumnos aprobados es del %d %%\n",ia);
ir=100-ia;
printf("\n\nEl indice de alumnos reprobados es del %d %%\n",ir);
return 0; }
<<Mentor's note: post edited for clarity. Please use code tags.>>
 
Last edited by a moderator:
Physics news on Phys.org
Mrencko said:
one teacher wants to know the % of fail and aproved of the class of "N" number of students,
Your program doesn't work for arbitrary N.

Mrencko said:
its almost done but the program don't show the "how much students failed the curse"
Could you please give more details as to what doesn't work?

Mrencko said:
C:
for(x=6;x>0;x--){
     if(a[x]<7){
     repro--;}
}
The index of a is out of bounds.
 
Ok more detailed, my program when runs display every thing exept for how much students failed the curse, i mean the program do the calculations for % of failed students but don't display the number of failed students
 
DrClaude said:
Could you please give more details as to what doesn't work?
i think its more a little error than a serious code break error
 
I have no problem running your code.
 
Fixes noted in comments. I'm not sure if there are any other issues.

C:
#include<stdio.h>
int main(){
int x,a[5],ia,ir,repro=0,apro=0;
for(x=0;x<5;x++){
     printf("ingrese la calificacion del alumno %d:\n",x+1);
     scanf("%d",&a[x]);
}
for(x=0;x<5;x++){
     if(a[x]>7){
         apro++;}
}
for(x=5;x>0;x--){        /* changed 6 to 5 */
     if(a[x]<=7){        /* changed < to <= */
         repro--;}
}
printf("\n\nLos alumnos aprobados son %d\n",apro);
printf("\n\nLos alumnos reprobados son %d\n",repro);
ia=(2*apro)*10;
printf("\n\nEl indice de alumnos aprobados es del %d %%\n",ia);
ir=100-ia;
printf("\n\nEl indice de alumnos reprobados es del %d %%\n",ir);
return 0; }

The second two loops could be combined:

Code:
for(x=0;x<5;x++){
     if(a[x]>7){
         apro++;}
     else{
         repro--;}
}
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
5K
Replies
9
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K