# Program to generate numbers, finding average and largest values and rounding the largest to 100

As this code is not in English, I will explain it first:

• Creates ten numbers in an array between 0 and 100 as student grades.

• Calculates the largest number and average value of these ten numbers in the array.

• 100, the largest grade, is subtracted from the largest number in the array. (e.g 100-96=4)

• New value (which is 4 in this case, explained in example above) is added to all numbers in the array. It's also printed just below average value and largest value.

• New values in the array that are unable to surpass the average value are printed down below, which prints a sentence in Turkish that roughly translates to:

"%dth student with %d grade has failed the lesson by staying under the average value."


#include<time.h>
#include<stdio.h>
#include<stdlib.h>

int main() {

int sum=0,fark,avg,imax;
int puan[10];
srand(time(NULL));
for(int i=0; i<10; i++)
{
puan[i]=rand()%101;
printf("%d. ogrencinin notu = %d ",i+1,puan[i]);
printf("\n");
if(i==0)
{
imax=puan[i];
}
else if(puan[i]>imax)
{
imax=puan[i];
}
sum += puan[i];
}
avg=sum/10;
fark= 100 - imax;
printf("\nEn yuksek not = %d ve not ortalamasi %d.\n",imax,avg);
printf("Her ogrencinin notuna %d puan eklendi.\n",fark);
printf("\n");
for(int i=0;i<10;i++)
{
puan[i] += fark;
}
for(int i=0;i<10;i++)
{
if(puan[i]<avg)
{
printf("%d. ogrenci aldigi %d notuyla ortalamanin altinda kalarak dersten basarisiz olmustur.\n",i+1,puan[i]);
}
}
return 0;
}


Can I shorten this code? Is there a shorter way to make it work in the same way? This was the best my friend and I could come up with.

Just a quick approach. It could be improved more. What you want is a way to reduce the two loops into one.

Some comments about the code: We initialized imax in order to reduce the if -else statement and also we combined the 2 foor loops in the end into one. What I would like to achieve even more is to reduce the final two for loops into one. But I think this is not possible for this case. But I might be wrong.

#include <time.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
int sum = 0, imax = 0;
int puan[10];
srand(time(NULL));
for(int i=0; i<10; i++)
{
puan[i]=rand()%101;
printf("%d. ogrencinin notu = %d \n",i+1,puan[i]);
if(puan[i]>imax)
{
imax=puan[i];
}
sum += puan[i];
}
float avg=sum/10.0f;
int fark = 100 - imax;
printf("\nEn yuksek not = %d ve not ortalamasi %f.\n",imax,avg);
printf("Her ogrencinin notuna %d puan eklendi.\n\n",fark);
for(int i=0;i<10;i++)
{
puan[i] += fark;
if(puan[i]<avg)
{
printf("%d. ogrenci aldigi %d notuyla ortalamanin altinda kalarak dersten basarisiz olmustur.\n",i+1,puan[i]);
}
}

return 0;
}

• Due to the nature of this kind of question, you can not reduce the loop to one. Two scans of the array is a must. Dec 13, 2017 at 21:57
• @phynju Yes! We needed a float to measure correctly the average! Nice touch. I like keeping the variables as close to where they are needed and not declaring at the top but that is not important.
– Tedi
Dec 13, 2017 at 23:00
• @chux Yes you are correct. Thanks. Changed it.
– Tedi
Dec 14, 2017 at 11:16
• Welcome to Code Review! You have presented an alternative solution, but haven't reviewed the code. Please edit it to explain your reasoning (how your solution works and how it improves upon the original) so that everyone can learn from your thought process. I can see quite a few improvements on the original that you haven't explained (e.g. initialising imax to avoid the if/else, combining printf() calls into a single one. It's not really a review if you don't provide the commentary that helps a developer improve. Dec 18, 2017 at 9:14

To shorten, initialize imax to the minimum value.

#define CLASS_N 10
#define SCORE_MIN 0
#define SCORE_MAX 100

int imax = SCORE_MIN;
for(int i=0; i < CLASS_N; i++) {
puan[i] = rand()%(SCORE_MAX - SCORE_MIN + 1) + SCORE_MIN;
printf("%d. " S_STUDENT_GRADE " = %d\n", i+1, puan[i]);

// if(i==0) no need for this test

if (puan[i] > imax) {
imax = puan[i];
}
sum += puan[i];
}


// for(int i=0;i<10;i++) { puan[i] += fark; }
for(int i=0; i < CLASS_N; i++) {
if (puan[i] + fark < avg) {
printf("%d. ogrenci aldigi %d ....\n", i+1, puan[i] + fark);
}
}


Unclear if OP's code is functionally correct. I would not expect the fark to change those students who are under the average. Either the average is incremented by fark or the 50% compare does not include fark.

Note also the float math is not needed. Still I do recommend FP.

printf("ve not ortalamasi %d.\n", sum/CLASS_N);
// or
int avg = sum*100/CLASS_N;
printf("ve not ortalamasi %d.%02d\n", avg/100, arg % 100);

...
if (puan[i]*CLASS_N < sum) {

• I think he/she wants to give some poor performance students Christmas bonus. What a kind teacher. Dec 14, 2017 at 4:08
• @phynju So with scores 30,30,30...,30,30,30,40, everyone one is above average? Kind perhaps. Dec 14, 2017 at 4:18
• So? Do you really want to fail all your lovely students? Dec 14, 2017 at 4:22
• Of course not, all my students are "above average". That 30,30,...40 was all lies and viscous gossip. Dec 14, 2017 at 4:29
• @CiaPan Yes - post amended. Dec 19, 2017 at 14:41