I need to initialize the array to a size of 10 instead of asing for user input.
ID: 3891878 • Letter: I
Question
I need to initialize the array to a size of 10 instead of asing for user input. How would I do so?
#include <stdio.h>
int main() {
// asking the size of array from user
int count;
printf("Enter the size of the array: ");
scanf("%d", &count);
printf(" ");
int arr[count], i;
printf("Enter %d numbers seperated by space: ", count);
// taking input of array values from user
for(i=0; i<count; i++)
scanf("%d", &arr[i]);
printf(" ");
int inversions = 0, j;
for(i=0; i<count-1; i++) {
for(j=i+1; j<count; j++) {
if(arr[j] < arr[i])
inversions++;
}
}
printf("The number of inversions is: %d ", inversions);
return 0;
}
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
int main() {
// asking the size of array from user
int count=10;
//printf("Enter the size of the array: ");
//scanf("%d", &count);
//printf(" ");
int arr[count], i;
//printf("Enter %d numbers seperated by space: ", count);
// taking input of array values from user
for(i=0; i<count; i++)
arr[i]= rand() % 1000;
printf(" ");
int inversions = 0, j;
for(i=0; i<count-1; i++) {
for(j=i+1; j<count; j++) {
if(arr[j] < arr[i])
inversions++;
}
}
printf("The number of inversions is: %d ", inversions);
return 0;
}
Output: