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

using C, Write a program that gets an integer as a command line argument and dyn

ID: 3674791 • Letter: U

Question

using C, Write a program that gets an integer as a command line argument and dynamically allocates an array of ints of that size. Fill the array with random integers less than 100. Display the contents of the array. Note that stdlib.h contains a function called atoi() that can be used to convert a string containing an integer to an actual integer.

notes: can not include conio.h, also the atoi() function must be used.

Sample Run: Command Prompt$ ./a.out 5 array[0] is 95 array[1] is 16 array [21 is 99 array [3] is 26 array [4] is 81

Explanation / Answer

Please find the required answer below :

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main( int argc, char *argv[] ) {

if( argc == 2 ) {

int size = atoi(argv[1]);

int arr[size];

for(int i=0; i<size; i++){
   srand(time(NULL));
   int r = (rand()%100 + 1);
   arr[i] = r;
}

for(i=0; i < size; i++ ){
   printf("%d, ",arr[i]);
}
} else {
printf("Invalid usage! ");
}
}