So I'm doing a course on C. One of the assignments is to look at and understand the code for sorting 5 grades(numbers). I copied the professor's code to MVS but it came up with errors. One of the issues I solved, as you can see by the comments in the code. But I don't understand how the professor managed to compile.
So my question is: how can the second error be solved?
int w[how_many];//error: expression must have a constant value
.
Anyone care to help?
I'm using Visual studio. Tried "disable language ext.", rename source file from .cpp to .c and changed to "compile as C code (/TC)"
#include <stdio.h>
#define SIZE 5
void print_array(int how_many, int data[], const char* str)
{
int i;
printf("%s", str);
for (i = 0; i < how_many; i++)
printf("%d\t", data[i]);
}
void merge(int a[], int b[], int c[], int how_many)
{
int i = 0, j = 0, k = 0;
while (i < how_many && j < how_many)
if (a[i] < b[j])
c[k++] = a[i++];
else
c[k++] = b[j++];
while (i < how_many)
c[k++] = a[i++];
while (j < how_many)
c[k++] = b[j++];
}
void mergesort(int key[], int how_many)
{
int j, k;
int w[how_many];//error: expression must have a constant value
for (k = 1; k < how_many; k *= 2)
{
for (j = 0; j < how_many - k; j += 2 * k)
merge(key + j, key + j + k, w + j, k);
for (j = 0; j < how_many; j++)
key[j] = w[j];
}
}
int main(void)
{
//const int SIZE = 5; // removed because of error and added #define SIZE 5
int a[SIZE] = { 67, 82, 83, 88, 99 };// error: needs constant value, solved by adding #define SIZE 5
print_array(SIZE, a, "My grades\n");
printf("\n\n");
mergesort(a, SIZE);
print_array(SIZE, a, "Sorted grades\n");
printf("\n\n");
return 0;
}