I need help filling out the document below. It is for my C and UNIX class so I\'
ID: 3746882 • Letter: I
Question
I need help filling out the document below. It is for my C and UNIX class so I'd need it written in C programming language. Thanks in advance for any help!
Exercises
Write statements to do the following:
a. Define a constant called MAX_SIZE having value 200.
b. Declare an array named n to hold MAX_SIZE int values.
c. Initialize each element of n to 1.
d. Assign value 5 to the last element in the array n.
e. Initialize element 0 as 0, element 1 as 1, …, element 199 as 199.
f. Display the sum of the first two elements in the array n.
g. Randomly generate an index (between 0 and MAX_SIZE – 1) and display the element of this index in the array n.
h. Write a loop to generate random integers between 0 and 999 and store them in the array n.
i. Sum all the elements of the array, using for loop. Declare the integer variable i as a control variable for the loop.
j. Sum all the elements of the array, using while loop. Declare the integer variable i as a control variable for the loop.
k. Write a loop that finds the minimum element in the array n.
l. Copy 11-element array a into the first portion of array b, which contains 34 elements. Assume that the elements of both arrays have the same type
m. Copy the elements of array a into an array b in the reverse order. Assume that both a and b contain MAX_SIZE elements and the elements of both arrays have the same type.
n. Calculate the occurrences of each number in the array n.
Explanation / Answer
a. Define a constant called MAX_SIZE having value 200.
Ans. const int MAX_SIZE = 200;
b. Declare an array named n to hold MAX_SIZE int values.
Ans. int n[MAX_SIZE];
c. Initialize each element of n to 1.
Ans.
int i;
for(i=0; i < MAX_SIZE; i++) {
n[i] = 1;
}
d. Assign value 5 to the last element in the array n.
Ans.
n [ MAX_SIZE - 1 ] = 5;
e. Initialize element 0 as 0, element 1 as 1, …, element 199 as 199.
Ans.
int i;
for(i=0; i < MAX_SIZE; i++) {
n[i] = i;
}
f. Display the sum of the first two elements in the array n.
Ans.
int sum = n[0] + n[1];
printf ( "%d" , sum );
g. Randomly generate an index (between 0 and MAX_SIZE – 1) and display the element of this index in the array n.
Ans. for this to work, you'll need to include the < stdlib.h > header file
srand ( time (0) ); // to seed the random number generator function
int randomIndex = rand() % MAX_SIZE ;
printf ( "%d", n[ randomIndex] );
h. Write a loop to generate random integers between 0 and 999 and store them in the array n.
Ans.
srand( time(0) );
int i;
for( i =0; i < MAX_SIZE; i++) {
n[i] = rand() % 1000;
}
i. Sum all the elements of the array, using for loop. Declare the integer variable i as a control variable for the loop.
Ans.
int i, sum = 0;
for( i=0; i < MAX_SIZE; i++ ) {
sum = sum + n[i];
}
j. Sum all the elements of the array, using while loop. Declare the integer variable i as a control variable for the loop.
Ans.
int i = 0, sum = 0;
while( i < MAX_SIZE ) {
sum = sum + n[i];
i = i + 1;
}
k. Write a loop that finds the minimum element in the array n.
Ans.
int i;
int min = n[0]; // assuming the first element of the array n is minimum value
for( i = 0; i < MAX_SIZE; i ++ ) {
if ( n[i] < min ) {
min = n[i];
}
}
l. Copy the elements of array a into an array b in the reverse order. Assume that both a and b contain MAX_SIZE elements and the elements of both arrays have the same type.
Ans.
int i, j;
for( i = 0, j = MAX_SIZE - 1; i < MAX_SIZE; i++, j-- ) {
b[i] = a[j];
}
Hope this helps...
BEST WISHES!!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.