Question

In: Computer Science

Write a program in C that declares the following array: int. array[] = { 1, 2,...

Write a program in C that declares the following array:

int. array[] = { 1, 2, 4, 8, 16, 32 }

Then write some code that accepts a number between 0 and 5 from the user and stores it in a variable called "index".

Write some code that retrieves the item specified by the index, like this:

int item = array[index];

Then write code that outputs the corresponding array entry based on the number the user entered. Example output:

The item at array index 3 is 8.

Now change the code that retrieves the item so that it looks like this:

int item = index[array];

And notice that you still get the same output. Why does this work? Is this good coding style? Explain in a brief paragraph after the final version of your code below and then add some comments to your program.

Solutions

Expert Solution

Here is the C program asked to write:

#include <stdio.h>

int main()
{
int array[] = { 1, 2, 4, 8, 16, 32 };
int index;
  
scanf("%d",&index); //taking input for the index variable
if(index >=0 && index<=5){ //program proceeds only if index is between 0 and 5 inclusive
int item = array[index]; //style 1
printf("The item at array index %d is %d\n",index, item); //printing the item retrived from style 1
item = index[array]; // changed to style 2
printf("The item at array index %d is %d\n",index, item); //printing the item retrived from style 2
}
return 0;
}

And the output we get is for input 4:

Actually , we get same output in both the cases because, of pointer arithemetic. Because index[array] and array[index] are finally represented as (index + arrayBasePointer) and (arrayBasePointer + index). In any of the ways, both are some. Here, arrayBasePointer is incremented by index number of integer blocks not index number of bytes. It is incremented by index number of integer blocks because, the arrayBasePointer is integer pointer. So, it will increment index number of elements from the arrayBaseAddress.


Related Solutions

C Programming Only Write a program that declares a one-dimensional array of integers with 24 elements....
C Programming Only Write a program that declares a one-dimensional array of integers with 24 elements. Fill the array with random integers (use a loop). Neatly output each element in the one-dimensional array. Next convert your one-dimensional array of 24 elements into a two-dimensional array of 6 x 4 elements. Neatly output each element of the two-dimensional array. The values will be identical to the one-dimensional array – you’re just converting from one dimension to two.
Write a method which is passed A[], which is an array of int, and an int...
Write a method which is passed A[], which is an array of int, and an int passingScore. The method returns the number of items in A[] which are greater than or equal to passingScore. Write a method which is passed an array of int A[]. The method returns true if A[] is the same backwards and forwards. Write a method same( ), which is passed two arrays of int. The method returns true if the two arrays contain exactly the...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Write another C++ function,lastLargestIndex, that takes as parameters an int array and its size and returns the index of the last occurrence of the largest element in the array. An analysis and design of the function smallestIndex is given below. Write an analysis and design for the function lastLargestIndex. Write...
Write a function that accepts an int array and the array's size as arguments.
Write a function that accepts an int array and the array's size as arguments. The function should create a copy of the array, except that the element values should be reversed int the copy. The function should return a pointer to the new array. Demonstrate the function in a complete program.  
(IN C) Program Question 2: Write a program that solves for c in the Pythagorean Theorem:...
(IN C) Program Question 2: Write a program that solves for c in the Pythagorean Theorem: a2 + b2 = c2 The user will enter values for a and b, and you will calculate c. All of this code will go in just one source file.
Write a Java program that will use a two-dimensional array to solve the following tasks: 1....
Write a Java program that will use a two-dimensional array to solve the following tasks: 1. Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. 2. Create a method to print the array. 3. Create a method to find the largest element in the array 4. Create a method to find the smallest element in the array 5. Create a...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array. getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
Program must be in C++! Write a program which: Write a program which uses the following...
Program must be in C++! Write a program which: Write a program which uses the following arrays: empID: An array of 7 integers to hold employee identification numbers. The array should be initialized with the following values: 1, 2, 3, 4, 5, 6, 7. Hours: an array of seven integers to hold the number of hours worked by each employee. payRate: an array of seven doubles to hold each employee’s hourly pay rate. Wages: an array of seven doubles to...
Use pseudocode to describe an algorithm for the procedure: int[][] find_pairs( int array[], int value ).
Use pseudocode to describe an algorithm for the procedure: int[][] find_pairs( int array[], int value ). Given a sorted array of distinct integers and a single integer value, find all pairs of indexes in the list whose product is equal to the value. Return the index pairs on the same row of a two-dimensional array. Note, m will be the number of rows and total number of pairs found, while 2 will be the number of columns along every row...
Write a C program that asks the user to enter 15 integer numbers and then store them in the array.
Write a C program that asks the user to enter 15 integer numbers and then store them in the array. Then, the program will find the second largest element in array and its index without sorting the array. For example, In this array {-55,-2,1, 2, -3, 0, 5, 9, 13, 1, 4, 3, 2, 1, 0}, the second largest element is 9 [found at index 7].
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT