USING C PROGRAM: Write a program Iab7.c as follows: Declare an array of size 5.
ID: 3757982 • Letter: U
Question
USING C PROGRAM:
Write a program Iab7.c as follows: Declare an array of size 5. Read 5 integers from the user and store them in the array. Using a for loop, print the content of the array. Verify that all the elements entered by the user are printed correctly. Modify your program so the array has size 10. Read 10 integers from the user and store them in the array. Then print the content of the array. How many changes did you have to apply to your program? Verify again that your program prints all the array content correctly. Further modify the program so that it works for array of any size (this is called Variable Length Array, VLA in short). Namely, the program will first ask the user for the size of the array, e.g., Then declare an array of size n after the scanf statement. Now read n integers from the user and store them in the array. Print the content of the array. Verify that your program prints all the array content correctly. Try to access the content that is out of the array index boundary, See what happens.Explanation / Answer
CODE :
// For array of size 5
int myArray [5] = {1,2,3,4,5};
/* To print all the elements of the array
for (int i=0;i<5;i++){
printf("%d", myArray[i]);
}
// For array of size 10
#include<stdio.h>
#include<conio.h>
#define MAX 30
void main() {
int size, i, arr[MAX];
int *ptr;
clrscr();
ptr = &arr[0];
printf(" Enter the size of array : ");
scanf("%d", &size);
printf(" Enter %d integers into array: ", size);
for (i = 0; i < size; i++) {
scanf("%d", ptr);
ptr++;
}
ptr = &arr[size - 1];
printf(" Elements of array in reverse order are :");
for (i = size - 1; i >= 0; i--) {
printf(" Element%d is %d : ", i, *ptr);
ptr--;
}
getch();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.