Write a program and declare an integer array named stuff with 50 elements, then
ID: 3571719 • Letter: W
Question
Write a program and declare an integer array named stuff with 50 elements, then fill it with 0, 1, 2, 3, ..., 49, 50 (i.e. initialize the first element of the array to 0, second element to 1, third element to 2, and so forth. you MUST use loops in here). Then ask the user to enter an index between 0 - 49, then ask for a number (int NUM). The program must include the number (NUM) at the indicated index of the array, and moves forward and includes N+1 in the next position (Index +1) and N+2 in position (Index +2) and so forth.
Here is an example of program's input and output:
int test[50];
Suppose array test contains the following numbers {0, 1, 2 , 3 , 4 , 5 , ... ., 49} // you must fill the array using a for loop.
Enter an index: 44
Enter a number: 70
Output array is: { 0, 1, 2, 3, 4, 5, ....., 42, 43, 70, 71, 72, 73, 74, 75} (use C++)
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{ /* defining an array stuff of size 50 */
int stuff[50];
/* variable to store number entered by the user */
int NUM;
/* variable to store index entered by the user */
int index;
/* filling elements in stuff array from 0 to 49 */
for(int i=0;i<50;i++)
stuff[i]=i;
/* prompting user to enter index */
cout << "Enter an index between 0 - 49 : ";
cin>>index; //storing user entered value in variable index
/* prompting user to enter number */
cout<<"Enter a number: ";
cin>>NUM; //storing user entered value in variable NUM
/* loop to update stuff array from user entered index to the last index of the array
from NUM value to NUM+1 with each iteration of loop */
for(int i=index;i<50;i++)
stuff[i]=NUM++;
/*printing output array */
cout<<"Output array is : ";
for(int i=0;i<50;i++)
cout<<stuff[i]<<" ";
return 0;
}
/*******OUTPUT*********
Enter an index between 0 - 49 : 44
Enter a number: 70
Output array is : 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 70 71 72 73 74 75
******OUTPUT***********/
/* Note:This code has been written in c++ and compiled on g++ compiler,Please do ask in case of any doubt,
would glad to help,Thanks!! */
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.