Hi, here is my problem...I created a program that finds the average of a user defined amount of numbers. It is a homework exercise to learn about dynamic memory (aka The heap), and the functions malloc() and free() .
This is what it does:
1)Asks user "How many numbers do you want to average ?".
2)Allocates just enough memory using malloc() to store all numbers + the average in an
int array.
3)Asks user for each number to be averaged, and stores them on the heap, also their total sum is stored in the last array block on the heap
4)The total sum is divided by the amount of numbers entered to get the average.Which is store where the total sum was stored(as the last array on the heap). total sum /= total amount of numbers.
5)print all numbers entered and their average.
6)wait for a character return, and then end the program
Code:
#include <stdio.h>
#include <stdlib.h>
main()
{
int *integers;
int num1;
int i;
printf("how many numbers do you want to average: ");
scanf("%d", &num1);
integers = malloc(num1 * sizeof(int));
if(integers == NULL)
{
printf("Malloc failed\n");
exit(1);
}
integers[num1] = 0;
for(i = 0; i < num1; i++)
{
printf("#%d Please enter a number: ", i + 1);
scanf("%d", &integers[i]);
integers[num1] += integers[i];
}
integers[num1] = integers[num1] / num1;
printf("The average of ");
for(i = 0; i < num1; i++)
printf("%d ", integers[i]);
printf("is: %d\n", integers[num1]);
free(integers);
fflush(stdin);
getchar();
}
It works great, homework exercise #2 officially : complete.
Until that is, i get it in to my head that, as a trivial (self imposed) academic exercise, i should break it up in to functions.Here it is:
Code:
#include <stdio.h>
#include <stdlib.h>
int allocate_memory(int *integers, int *num1);
int average_of(int *integers, int *num1);
int print_average(int *integers, int *num1);
allocate_memory(int *integers, int *num1)
{
printf("How many numbers do you want to average: ");
scanf("%d", num1);
integers = malloc(*num1 * sizeof(int));
if(integers == NULL)
{
printf("malloc has failed\n");
exit(1);
}
}
average_of(int *integers, int *num1)
{
int i;
integers[*num1] = 0;
for(i = 0; i < *num1; i++)
{
printf("#%d Please enter a number: ", i + 1);
scanf("%d", &integers[i]);
integers[*num1] += integers[i];
}
integers[*num1] /= *num1;
}
print_average(int *integers, int *num1)
{
int i;
printf("The average of ");
for(i = 0; i < *num1; i++)
printf("%d ", integers[i]);
printf("is : %d\n", integers[*num1]);
}
main()
{
int *integers;
int num1;
allocate_memory(integers, &num1);
average_of(integers, &num1);
print_average(integers, &num1);
free(integers);
fflush(stdin);
getchar();
}
This compiles!!!
But when i run it, it opens a console window, and prints "How many numbers do you want to average: ". Then, after you enter a number, it crashes. I can't figure it out... all day i've tried. I don't understand...am i not using pointers correctly? Why does the first program work, but not the second. Please, any help is appreciated.
I'm still learning C so, i apologize in advance if my question seems stupid to anyone.
If you can't explain it simply, you don't understand it well enough.
Albert Einstein