Answer to Question #194403 in C for Hassam

Question #194403

Your task is to perform some functions on integer arrays. Specifically you will write a C program

that does the following:

1. Declare an array of size 20.

2. Initialize the array with random values (use loop, and rand() function).

3. Print all the elements in the array.

4. Print all the elements in the array in the reverse order.

5. Print the array such that every Nth element gets printed. N is user input.


1
Expert's answer
2021-05-18T07:44:51-0400
#include <stdio.h>  // Used for printf()
#include <stdlib.h> // For random function
#include <time.h>   // For time()

#define SIZE 20 // Size of array

int main()
{
	int i;

	// 1. Declare an array of size 20.
	int array[SIZE];

	// 2. Initialize the array with random values
	// (use loop, and rand() function).
	srand(time(0)); // Initialize random numbers
	for (i = 0; i < SIZE; i++)
		array[i] = rand() % 100; // Random 0 .. 100


	// 3. Print all the elements in the array.
	for (i = 0; i < SIZE; i++)
		printf("%2d ", array[i]); // %2d insert space if n < 10
	printf("\n");

	// 4. Print all the elements in the array in the reverse order.
	for (i = SIZE - 1; i >= 0; i--)
		printf("%2d ", array[i]);
	printf("\n");

	// 5. Print the array such that every Nth element gets printed.
	// N is user input.
	int n;
	printf("Enter N: ");
	scanf("%d", &n);
	for (i = n - 1; i < SIZE; i += n)
		printf("%2d ", array[i]);
	printf("\n");

	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS