IN C++ Here is the source code it is referencing #include <iostream> using names
ID: 3741835 • Letter: I
Question
IN C++
Here is the source code it is referencing
#include <iostream>
using namespace std;
int main ()
{
//a common way to define array sizes - as a constant because
//you then use the constant in the array declaration and in any for loops
const int array_size = 4; //this is the size or # of elements of the array
int numbers[array_size]; //declare the array
int index; //position in the array
int max; //the highest number in the array
int max_position = -1; //the position of the highest number, -1 to default
//tells you that no maximum has been found at the start
//because the program hasn't started yet
int sum;
//assign the elements in the array
numbers[0] = 3;
numbers[1] = 2;
numbers[2] = 7;
numbers[3] = 6;
// The values in the array are summed.
sum = 0;
for (index = 0; index < array_size; index++)
sum = sum + numbers[index];
cout << "Sum is " << sum << endl;
//find the maximum integer in the array
max = 0;
cout << "before searching, this is the array: " <<
numbers[3] <<
numbers[2] <<
numbers[1] <<
numbers[0] << endl;
cout << "the max number and position, not found yet are: " << max << " " << max_position << endl;
for (index = array_size-1; index >= 0;index--) {
//start at the top and go down or start at the bottom and go up
//makes no difference
if (numbers[index] > max) {
max = numbers[index];
max_position = index;
}
}
cout << "max is " << max << " at index " << max_position << endl;
//do a logical shift left or "up" replacing the lowest index with a 0
//and shifting the number in the highest position out
cout << "before shifting, this is the array: " <<
numbers[3] <<
numbers[2] <<
numbers[1] <<
numbers[0] << endl;
int lower_index = 0; //keep track of the next position down
for (index = array_size-1; index >= 1;index--) {
//don't go all the way to zero, we'll do that outside the loop at the end
lower_index = index-1;
numbers[index] = numbers[lower_index];
}
numbers[0] = 0;
cout << "after shifting, this is the array: " <<
numbers[3] <<
numbers[2] <<
numbers[1] <<
numbers[0] << endl;
return 0;
}
Write basic array programs. In class, there was an example to introduce arrays. It's in week6 D2L under week6 - basic_array. Download that zip file and find that project. In that program an array is defined and a few operations are performed (find the sum of the elements, find the position of the highest number and a shift up). Run and experiment with that program to understand what it does before you start writing your program. Copy the source code into your new program and do the following:
a. Expand the size of the array to 10 elements and assign values to all 10 elements
b. Include this code in your program. This function prints out the contents of any integer array:
void printarray(int x[], int length) {
for(int y=length-1;y>=0;y--)
cout << x[y] << " ";
cout << endl;
}
c. Write code to find the average of all the array elements
d. Write code that asks the user for an integer value and then search the array for that value. Print out the position if found, if the user value is not in the array, print out a message that it was not found.
e. The example I did in class did a shift in one direction. The notion of shift left or right depends how how you, the human, choose to interpret or print out the array. Should the array start printing with the highest index or start with the lowest index? It's up to you. Write a program section that shifts down, i.e., the highest index position is replaced by a 0 and the lowest index position is shifted out and discarded. This is like a logical shift right if you are printing out from highest to lowest.
f. Create a function that does the logical shift left (or up) to any number of spaces "n" (as long as n is not bigger than the array itself). The function should accept an integer array of any size, the length and the n, the number of elements to shift. Move your code from the step above into the function definition. You specify an array of any size in the function parameters with "[]" - for example:
void function1(int ar[]) {
and the function call would appear as:
function1(numbers);
By definition, arrays passed to functions are always pass-by-reference. Any changes you make to the array in the function are made to the original array. No local copy is created. As an example if my function is called "shiftleft" and my array has 8 elements and is called "numbers", here is the function call:
shiftleft(numbers, 8, 2) and the resulting sample output of before and after the function call:
Note that the printarray function is displaying the highest index element first and the lowest index element last. This means the original array was:
index 7 6 5 4 3 2 1 0
value 8 7 6 5 4 3 2 1
the array after shifting becomes:
index 7 6 5 4 3 2 1 0
value 6 5 4 3 2 1 0 0
SIGNOFF CRITERIA: You should have one program that finds the average, searches the array, right shifts and has a function that does a variable left shift of a 10 element array
Explanation / Answer
/// for left shifting you have to just reverse the loop ;)
#include <iostream>
using namespace std;
void shift( int arr[],int n );
int main()
{
int arr[10], i, num, n, c=0, pos,sum,average;
sum=0;
cout<<"Enter the array size : ";
cin>>n;
cout<<"Enter Array Elements : ";
for(i=0; i<n; i++)
{
cin>>arr[i];
sum+=arr[i];
}
cout<<"Enter the number to be search : ";
cin>>num;
for(i=0; i<n; i++)
{
if(arr[i]==num)
{
c=1;
pos=i+1;
break;
}
}
if(c==0)
{
cout<<"Number not found..!!";
}
else
{
cout<<num<<" found at position "<<pos;
}
average=sum/n;
cout<<" THE AVERAGE OF THE GIVEN ARRAY IS:"<<average;
cout<<" THE GIVEN ARRAY IS:";
for (int i = 0; i < n; i++)
{
cout<<arr[i];
}
int a;
int lower_index;
cout<<" THE ARRAY AFTER SHIFTING RIGHT IS :";
a = 0;
for (a = n-1; a >= 1;a--) {
lower_index = a-1;
arr[a] = arr[lower_index];
}
arr[0] = 0;
for (int i = 0; i < n; i++)
{
cout<<arr[i];
}
return 0;
}