### Thread: Need correction on my code!!! (My 1st post)

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2013
Posts
4
Rep Power
0

#### Need correction on my posted code!!! (My 1st post)

Hello friends. I am practicing and learning C.

I have the following code.

* the program looks for a number of random integers.
* allocates an array and fills it with numbers between 1 and 500.
* figures out the smallest, biggest and the average.
* and then frees memory.

(i found this code in my book in heap chapter...so i need the pointer and malloc and free to be there.)

here are the codes.
The problem is Output is not accurate. sometimes average and smallest value gives me MINUS value.

what could be the problem in the following code please?

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

main()
{
int i, aSize;

int * randomNums;

time_t t;

double total = 0;
int biggest, smallest;
float avg;

srand(time(&t));

printf("How many random number do you want in your array? ");
scanf(" %d", &aSize);

randomNums = (int *) malloc(aSize * sizeof(int));

if(!randomNums)
{
printf("Random array allocation failed!\n");
exit(1);
}

for (i = 0; i < aSize; i++)
{
randomNums[i] = (rand() % 500) + 1;
}

biggest = 0;
smallest = 500;

for(i = 0; i < aSize; i++);
{
total += randomNums[i];
if (randomNums[i] > biggest)
{
biggest = randomNums[i];
}
if (randomNums[i] < smallest)
{
smallest = randomNums[i];
}
}

avg = ((float)total)/((float)aSize);

printf("Biggest number is %d \n", biggest);
printf("Smallest number is %d \n", smallest);
printf("Average number is %.2f \n", avg);

free(randomNums);

return(0);

}```
2. C:TEST>gcc -Wall arifudden_1.c
arifudden_1.c:6: warning: return-type defaults to `int'

C:TEST>
You need to explicitly declare main to return int.

Other than that, do you have a question? The topic title suggests that you think that this code (which you admit is not your code, so that part of the title was not truthful) needs to be corrected. But you give us no other reason to think that there needs to be a correction. You do not identify, let alone describe, any problem. You don't even ask a question. Are you expecting an answer? Well, how can you expect an answer if you do not pose any question?

Do you have a question?

• arifuddin agrees : absolutely right.
3. PS

Code:
```    for(i = 0; i < aSize; i++);
{
total += randomNums[i];
if (randomNums[i] > biggest)```
Did you intend for that semicolon to be there?

• arifuddin agrees : solved my problem 100%.
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2013
Posts
4
Rep Power
0
thanks a lot man. i am very new here. also did realized i what you have just mentioned. i will keep all these in mind and correct my approach.
5. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2013
Posts
4
Rep Power
0
dwise aol. thanks again for your correction. that one semi colon stuck me on the same place for the last two days. i became kind of frustrated.