Write C++ statements to accomplish each of the following

Question

Write C++ statements to accomplish each of the following:
(a) Output the value of the seventh element of character array a.
(b) Input a value into element 4 of one-dimensional double array b.

Explanation

Here in this question, we have to accomplish C++ statements

The array is a term that is used to store data of some kind. It is declared at the starting. Once it is declared its size is fixed.

Syntax to declare an array is :
datatype a[Size of array];

So the example is:
char a[10]; // here we have an array of 10 values of character
int a[10]; //And here array of integer to store 10 int values

In the same way, we can create float and double and structure arrays also.

And in array indexing is always starts from the number0. So that if you want any number from the index then look it for the n-1 index number.

For a[6], indexing is done as,

Index 0 1 2 3 4 5
Element 1 2 3 4 5 6

So in the first question, we want the 7th element so we have to find it on the 6th index. By the logic of n-1.

Code for the first part:

#include<iostream>
using namespace std;
// Main method for array
int main()
{
    char a[]="MyStringArray";
    cout<<"7-th element of the char array "<<a<<" is "<<a[7-1];
    return 0;
}

Output

C++ statements to accomplish

Code for the second question is :

#include<iostream>
using namespace std;
// Main method of array
int main()
{
    double a[]={1.0,2.1,3.2,4.3,4.5,6.5};
    cout<<"\nEnter value for 4th element of array: ";
    cin>>a[3];
    cout<<"\nArray is: ";
    for(int i=0; i<6; i++)
        cout<<" , "<<a[i];
    return 0;
}

Output

C++ statements to accomplish

 

Also read, find the shortest path in the multistage graph as follows.

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *