191k views
2 votes
9.4 Code Practice:Question 1

9.4 Code Practice:Question 1-example-1
User Tanyia
by
4.6k points

1 Answer

6 votes

Answer:

// Program is written in C++

// Comments are used for explanatory purpose

// Program starts here

#include<iostream>

using namespace std;

int main()

{

// Declare array

/* There are 18 characters between f and w. So, the array is declared as thus;*/

char letters [18];

// Initialise array index to 0

int i = 0;

// Get input

for(char alphs = 'f'; alphs<='w'; alphs++)

{

letters[i] = alphs;

// Increment array elements

i++;

}

// Print Array

cout<<"['";

for(int j = 0; j <18; j++)

{

if(j<17){

cout<<letters[j]<<"', '";

}

else

{

cout<<letters[j]<<"']";

}

}

return 0;

}

Step-by-step explanation:

The above code declares a char array named letters of length 18. This is so because there are 18 characters from f to w (both inclusive). This is done using the following: char letters [18];

Index of array starts from 0; so, the next line initializes the array letters to 0 and prepares it for input.

The first for-loop statement is used to input characters to letter array. This done by iterating from 'f' to 'w' using variable alphs.

Outside the for loop; the statement cout<<"['"; prints ['

The next for loop prints f','g','h' ........ 'w']

Bring this two prints together, it gives the desired output

['f','g','h' ........ 'w']

User Chevul Ervin
by
5.4k points