Inoduction to Arrays Introduction to Arrays An array is a variable capable of st
ID: 3762284 • Letter: I
Question
Inoduction to Arrays
Introduction to Arrays An array is a variable capable of storing multiple values. When we declare an array we tell the compiler how many values we want the array to hold. We also tell the compiler what type of values the array can store. All of the values in an array must be of the same type. Here is a declaration of an array called numlist that will be used to store 8 integers 1. int numlist[8] I/ declare int array that can store 8 values If we wish to fill the array numlist with the integers typed from the keyboard, you can use a for loop. Here is a for loop that will allow you to enter 8 values from the keyboard and will store them in the array numlist. Notice that we have used the variable i as an index for array numlist.Explanation / Answer
#include <stdio.h>
void intSwap(int *x, int *y);
int getIntArray(int a[], int nmax, int sentinel);
void printIntArray(int a[], int n);
void reverseIntArray(int a[], int n);
int main(void) {
int x[10];
int hmny;
hmny = getIntArray(x, 10, 0);
printf("The array was: ");
printIntArray(x,hmny);
reverseIntArray(x,hmny);
printf("after reverse it is: ");
printIntArray(x,hmny);
}
void intSwap(int *x, int *y)
/* It swaps the content of x and y */
{
int temp = *x;
*x = *y;
*y = temp;
}
/* n is the number of elements in the array a.
* These values are printed out, five per line. */
void printIntArray(int a[], int n){
int i;
for (i=0; i<n; ){
printf(" %d ", a[i++]);
if (i%5==0)
printf(" ");
}
printf(" ");
}
/* It reads up to nmax integers and stores then in a; sentinel
* terminates input. */
int getIntArray(int a[], int nmax, int sentinel)
{
int n = 0;
int temp;
do {
printf("Enter integer [%d to terminate] : ", sentinel);
scanf("%d", &temp);
if (temp==sentinel) break;
if (n==nmax)
printf("array is full ");
else
a[n++] = temp;
}while (1);
return n;
}
/* It reverse the order of the first n elements of array */
void reverseIntArray(int a[], int n)
{
int i;
for(i=0;i<n/2;i++){
intSwap(&a[i],&a[n-i-1]);
}
}