Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Programming. Basic knowledge only. Please help. using cout 25 . For the firs

ID: 3885533 • Letter: C

Question

C++ Programming. Basic knowledge only. Please help.

using cout

25. For the first part, implement the source code to display an output as:

1 2 3 4

using cout, endl, , in the following consecutive versions:

1 – using one ‘cout‘ statement, with the numbers as one string;

2 - using one ‘cout‘ statement, with four cascaded string-inputs;

3 - repeat, using integer numbers as four inputs (you must insert spaces as " ";

4 - repeat, using characters;

5 - using four individual ‘cout’ statements, and using only one string at a time (insert space-separator in every string).

26. At the end of this first part, and also at the end of all following sections, pause using:

system("pause"); // it displays: Hit any key to continue   

system("cls");

Second part : using ‘printf_s()’

--------------------------------

27. Use ‘printf_s()’ instead of cout’, for all above implementation versions.

Before you start adding the new code, comment out the code already implemented for ‘cout’, using the preprocessor directives

#if and #endif.

And do the same thing for all sections to be implemented next.

In the end, de-activate the two preprocessor-directives using // in front of them, to run all implemented-sections, from the top to the end of the project-source-code.

Here are some implementation-examples for this second ‘printf’

section:

printf("%d %d ", 1, 2); // space inside the control-string

printf("%s%s ", "1 ", "2 "); // space inside the

// displayable strings

You may also use

scanf_s("%c", &ch, 1); // to insert a “pause”

instead of system("pause");

Note: the old version scanf_s("%c", &ch) is no longer recommended. Try it. You get an warning: not enough arguments passed.

Third part : display a table

Explanation / Answer

25.

// Example program
#include <iostream>
#include <string>
using namespace std;

int main()
{
string numbers="1234";
string numarray[4];
numarray[0]="str1";
numarray[1]="str2";
numarray[2]="str3";
numarray[3]="str4";
int intarray[4]={1,2,3,4};
char chararray[4]={'a','e','i','o'};
cout<<" String with the numbers "<<numbers;
cout<<" Strings with cascading "<<numarray[0]<<" "<<numarray[1]<<" "<<numarray[2]<<" "<<numarray[3];
int length=(sizeof(intarray)/sizeof(*intarray));
cout<<" repeated array ";
for(int i=0;i<length;i++)
{
cout<<intarray[i]<<" ";
}
int charlength=(sizeof(chararray)/sizeof(*chararray));
cout<<" repeated array ";
for(int i=0;i<charlength;i++)
{
cout<<chararray[i]<<" ";
}
cout<<" indidual strings with four cout statements "<<endl;
cout<<numarray[0]<<" ";
cout<<numarray[1]<<" ";
cout<<numarray[2]<<" ";
cout<<numarray[3];
system("pause");
system("cls");
}

Output:

String with the numbers 1234

Strings with cascading str1 str2 str3 str4

repeated array

1 2 3 4

repeated array

a e i o i

ndidual strings with four cout statements

str1 str2 str3 str4

27.

// Example program
#include <iostream>
#include <string>
using namespace std;

int main()
{
string numbers="1234";
string numarray[4];
numarray[0]="str1";
numarray[1]="str2";
numarray[2]="str3";
numarray[3]="str4";
int intarray[4]={1,2,3,4};
char chararray[4]={'a','e','i','o'};

/*cout<<" String with the numbers "<<numbers;
cout<<" Strings with cascading "<<numarray[0]<<" "<<numarray[1]<<" "<<numarray[2]<<" "<<numarray[3];
int length=(sizeof(intarray)/sizeof(*intarray));
cout<<" repeated array ";
for(int i=0;i<length;i++)
{
cout<<intarray[i]<<" ";
}
int charlength=(sizeof(chararray)/sizeof(*chararray));
cout<<" repeated array ";
for(int i=0;i<charlength;i++)
{
cout<<chararray[i]<<" ";
}
cout<<" indidual strings with four cout statements "<<endl;
cout<<numarray[0]<<" ";
cout<<numarray[1]<<" ";
cout<<numarray[2]<<" ";
cout<<numarray[3];
system("pause");
system("cls");*/
  
// Code for printf
printf(" The below statements are printed using the printf");
string someString("1234");
printf(" String with the numbers %s",someString.c_str());
printf(" Strings with cascading %s%s%s%s",numarray[0].c_str(),numarray[1].c_str(),numarray[2].c_str(),numarray[3].c_str());
  
int prilength=(sizeof(intarray)/sizeof(*intarray));
printf(" repeated array ");
for(int i=0;i<prilength;i++)
{
printf("%d",intarray[i]);
printf(" ");
}
int charlength=(sizeof(chararray)/sizeof(*chararray));
cout<<" repeated array ";
for(int i=0;i<charlength;i++)
{
cout<<chararray[i]<<" ";
}
printf(" indidual strings with four cout statements ");
printf(" ");
printf("%s",numarray[0].c_str());
printf(" ");
printf("%s",numarray[1].c_str());
printf(" ");
printf("%s",numarray[2].c_str());
printf(" ");
printf("%s",numarray[3].c_str());
}

Output:

The below statements are printed using the printf

String with the numbers 1234

Strings with cascading str1str2str3str4

repeated array 1 2 3 4

repeated array a e i o

indidual strings with four cout statements str1 str2 str3 str4

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote