Should I be using a long for the array or a float?
Should I be making the array size larger?
When I use a larger array, say for instance 12 numbers, the program does not abort but instead prints a minus figure as the first item when its been sorted.
I know this is a memory issue but I'm just not entirely sure how to fix it.
Some explanation would be great!
PS. I am new to C. I don't remember encountering any issues like this with python.
#include <stdio.h>
void print_grades();
void average();
void swap();
void bubble_swap();
int main()
{
const int SIZE = 10;
int grades[SIZE] = { 67, 56, 65, 76, 32, 14, 59, 34, 6, 77 };
print_grades(grades, SIZE);
average(grades, SIZE);
bubble_swap(grades, SIZE);
return 0;
}
// This function prints the array
void print_grades(int grades[], int size)
{
int i;
printf("My grades are:\n");
for (i = 0; i < size; i++)
{
printf("%i\t", grades[i]);
}
}
// This function calculates the average of the array
void average(int grades[], int size)
{
int i;
double sum = 0.0, average;
printf("\nMy average grade is: ");
for (i = 0; i < size; i++)
{
sum += grades[i];
}
printf("%.1f\n", sum/size);
}
// This function swaps two ints
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
// This is the bubble sort function.
void bubble_swap(int grades[], int size)
{
int i, j;
for (i = 0; i < size; i++)
for (j = 0; j < size; j++)
{
if (grades[j] < grades[j+1])
{
NULL;
}
else
{
swap(&grades[j], &grades[j+1]);
}
}
printf("\nMy sorted grades are:\n");
for (i = 0; i < size; i++)
{
printf("%i\t", grades[i]);
}
}