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

IN C PROGRAM int check_error(int): This function takes an integer number and c h

ID: 3756761 • Letter: I

Question

IN C PROGRAM

int check_error(int):

This function takes an integer number and c

hecks if the number is between 1

-

50

or not

,

if it is, it returns 1 otherwise 0.

void initialize_array(int[], int ):

This func

tion takes an integer array and its size as an arguments and

initialize the integer 1D array with random numbers between 0

-

9.

Use rand() to do this. Also

remember where to use srand(). HINT:

use

for

-

loop.

void print_array(int [], int):

Like the function above it takes an integer array and its size and prints

the content of an array.

HINT:

use for

-

loop.

main():

First create a local v

ariable integer array of size 5

0 in main. Use this integer array to s

tore the

random numbers. Next,

read t

he size of input and

check if the size is less than 1

or greater than 5

0 using

check_error function, if the input from the user is invalid than display the error message and ask for the

input again repeat this until user provides with the valid size. Call

initialize_array function to initialize the

input array with random numbers. Call print function to display the content

s

of the array

FOR LOOP IS A MAJOR TOOL FOR YOU NEXT WEEK

Sample Output

Characters in bold are the input from the user

[sm3z5@babbage L

ab6

]$ ./a.out

Enter the size of the input:

0

Invalid input enter the size of the input again:

-

12

Invalid input enter the size of the input again:

112

Invalid input enter the size of the input again:

8

Input array

1

5 7 1 7 1 4 4 6

[sm3z5@babbage

Lab6

]$ ./a.out

Enter the size of the input:

6

Input array

1

9 0 2 4 0 5

Explanation / Answer

//Include required header files first .

/*Header files are basically files which contain information regarding syntax that you use, for the computer to understand*/

#include <stdio.h>/*This header file contains the instructions needed for the computer to perform input and output operations.*/

#include <stdlib.h>/*This header file contains the required rand() and srand() functions.*/

#include <time.h>/*This is required for seeding the srand() function.*/

/*Next we define all the required functions*/

/*This function checks whether the number is between 0 to 50 or not*/

int check_error(int i)

{

/*Check whether it is both greater than 0 and less than 51 as the condition is between 1 to 50*/

if(i > 0 && i < 51)

{

return 1;

}

/*If the condition is satisfied the function returns 1 and stops (remember that a function stops executing after the return call).*/

/*Else it returns 0.*/

return 0;

}

/*Next we initialize the array with the random number ranging from 0 to 9.*/

void initialize_array(int a[],int size)

{

/*If you want different random number each time you run the program just remove the two backslashes in the next line*/

//srand(time(0));

/*The above line is called seeding.A seed is a number which is generally used to initialize a random number generator.The above line is standard method of seeding rand() function.*/

for(int i = 0; i < size; i++)

{

/*Using rand() function to initialize the array*/

a[i] = rand() % 10;

/*Note that rand() function returns a number between 0 and 32767(or greater). So to get a number between 0 to 9 we divide rand() by 10 and take its remainder as any number divided by 10 leaves a remainder ranging from 0 to 9*/

/*But this generation is not completely random as a computer a can never truly generate a random number.Hence this is called pseudo-random number generation.Also using rand() we get the same series of numbers so if we want the numbers to be different in each run we use srand() which generates different numbers each run.*/

}

}

/*Next we create a function to print the numbers in the array*/

void print_array(int a[], int size)

{

/*This is done by running a for loop and printing a number in the array each iteration*/

for(int i = 0;i < size; i++)

{

printf("%d ", a[i]);

}

printf(" ");/*This just prints a new line*/

}

/*Now we create our main() function*/

int main()

{

int a[50];//Create an array of size 50.

int n;//This stores the size of the array.

printf("Enter the size of the input: ");

scanf("%d", &n);

/*Now we check whether the input is between 1 to 50 or not.For this we use a while loop until this condition is satisfied*/

while(check_error(n) == 0)

{

printf("Invalid input enter the size of the input again: ");

scanf("%d", &n);

}

/*Now we call the initialize_array function*/

initialize_array(a, n);

  /*Then we print by calling print_array() function*/

print_array(a, n);

return 0;

}