Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

By trial and error, discover what the largest value of x can be in the following

ID: 648471 • Letter: B

Question

By trial and error, discover what the largest value of x can be in the following program so that it still compiles and runs without error:main(){

int array[x][x];
array[0][0] = 0;
}

Print this value of x at the start of your submission. Then use malloc to create a 5000 by 5000 square array. Fill this array with random numbers in the range [0, 6]. Iterate through the array again, counting the number of times each value appeared.

Sample session (note: for the purpose of this sample, the value of x is incorrect. You should discover this value on your own.)
> ./a.out
x = 12
there are 3570129 0s
there are 3570751 1s
there are 3571904 2s
there are 3573195 3s
there are 3571223 4s
there are 3568653 5s
there are 3574145 6s

[Please answer in C lanugage. Thanks!]

Explanation / Answer

#include <stdio.h>

int count(int arr[], char alreadyPresent[], int num_elements, int val);
void printElements(int arr[], int num_elements);

int main(void)
{
int r = 20,i,*arr;
arr = (int *)malloc(sizeof(int)*20);
for(i = 0; i < 20; i++)
{
arr[i] = rand()%7;
}

char alreadyPresent[20] = {0}; /* initialize all elements to 0 */
int num_occ;

printf(" Array: ");
printElements(arr, 20);

for (i = 0; i < 20; i++)
{
num_occ = count(arr, alreadyPresent, 20, a[i]);
if (num_occ) {
alreadyPresent[i] = 1;
printf("The val %d was found %d times. ", arr[i], num_occ);
}
}
}

int count(int arr[], char alreadyPresent[], int num_elements, int val)
{
int i, count = 0;

for (i = 0; i < num_elements; i++)
{
if (arr[i] == val)
{
if (alreadyPresent[i] != 0) return 0;
++count; /* it was found */
}
}
return (count);
}

void printElements(int arr[], int num_elements)
{
int i;
for (i = 0; i<num_elements; i++)
{
printf("%d ", arr[i]);
}
printf(" ");
}