-
User input array size!!
Hi, i have an assignment to do that is: Write a program that asks the users to enter a value for array size “n” and fill up the array with n integers. Then reverse the array and print it on the screen.
I am not asking for the whole thing to be done for me, but I need help with getting the user to enter the array size. I have this so far
Code:
#include <stdio.h>
int main(void)
{
int n;
printf("Enter size of array\n");
scanf_s("%d", &n);
float array[n];
for (int i = 0; i < n; i++) {
scanf_s("%f", &array[n]);
}
printf("%f", array[n]);
getchar();
getchar();
}
-
You got correctly the data inside the array. Now you can write reverse function by using temporary array and then print the array.
-
This code does not work. It says "n" must have a constant value
-
That is because you have to have fixed array size
Also the declaration should come at the beginning of function.
-
You need to write
float *array = malloc( n * sizeof(*array) ); // synthesise float array[n];
Don't forget to include stdlib.h
Also, if you get warnings about casting void*, then stop using a C++ compiler to compile C code, by renaming your source files.
> scanf_s("%f", &array[n]);
Also, use i (your loop variable), not n (your array length).