Question

In: Computer Science

C++ Program - Arrays- Include the following header files in your program:     string, iomanip, iostream Suggestion:...

C++ Program - Arrays-

Include the following header files in your program:     string, iomanip, iostream
Suggestion: code steps 1 thru 4 then test then add requirement 5, then test, then add 6, then test etc.

Add comments to display assignment //step 1., //step 2. etc. This program is to have no programmer created functions. Just do everything in main and make sure you comment each step so I can grade more easily. Also, this program will be expanded in Chapter 9 to use pointers.

Create a program which has:
1. The following arrays created:
                a. an array of double with 5 elements, dArr
                b. an array of long, lArr, with 7 elements and initialized at the time of creation with the values
                                                100000, 134567, 123456, 9, -234567, -1, 123489
                c. a 2 dimensional array of integer, with 3 rows and 5 columns, iArr.
                d. an array of char with your name initialized in it. Big enough for 30 typable characters, sName.
2. define 3 variables, , cnt1 and cnt2 (short data types) as general purpose counters and a long double total
3. define 1 long variable called highest
4. a for loop to put a random number into each of the elements of the array of double, dArr. Use rand() and seed a random starting point with srand(). Use a for loop to display all of the values in dArr.              
5. another for loop to add up the array of double, dArr, into the variable total
6. one cout to print the total and another cout to print the average of the double array, dArr.
7. a for loop similar to the following for the long array, lArr:
                                for ( cnt1 = 1, highest = lArr[0] ; cnt1 < 7 ; cnt1++ )
                                {
                                                //logic to compare each array element, starting with lArr[1], with highest
                                                //replace highest if the value in lArr[cnt] is higher than the value in variable highest
                                }

8. a cout to print highest as derived in the above code        
9. a for loop to put a random number, each with a value no lower than 1 and no higher than 53, into each element of iArr, the array of integer, seed the random generator with srand( (unsigned) time(NULL)). Only have to run srand once…. Use the modulo operator similar to the way you did with dice rolls in Project 2.
10. a separate loop to print iArr with 3 rows on your screen. Each row has 5 numbers. Use setw to control the width of each column. See Chapter 3 for an example of a program using setw. Print row by row.
11. a loop to print the 2 dimensional array, iArr, so that all 3 numbers in column 0 are printed and then on the next line all 3 numbers in column 1, etc. thru column 4. Print column by column.
12. Use cin.getline( ...... ) to type another name into the variable sName.
13. Print the ascii value of each character in the char array, 1 per line. Use a while loop and look for the '\0' as a signal to end.
14. make the array of char, sName, have the name "Albert Einstein" in it. You must use strcpy_s function.
15. print the ascii value of the 12th character of the string sName

Solutions

Expert Solution

Code -


#include <iostream>
#include <istream>
#include <string>
#include <iomanip>
#include <cstring>
using namespace std;

int main()
{
//variable declare
double dArr[5];
double lArr[7] = {100000, 134567, 123456, 9, -234567, -1, 123489};
int iArr[3][5];
char sName[30] = {'J','o','h','n'};
short cnt1,cnt2;
long double total=0;
long highest;
// 4
int i;
//random number generate and store in dArr
srand(time(0));
for(i = 0 ; i<5 ;i++){
double f = (double)rand() / RAND_MAX;
dArr[i] = 1 + f * (49);
}
//print the dArr
for(i = 0 ; i<5 ;i++){
cout<<dArr[i]<<" ";
}
cout<<endl;
//do total of dArr
for(i = 0 ; i<5 ;i++){
total+=dArr[i];
}
//print the total
cout<<"Total of double array is "<<total<<endl;
//print the average
cout<<"Average of double array is "<<total/5<<endl;
//extract the highest from lArr and
for ( cnt1 = 1, highest = lArr[0] ; cnt1 < 7 ; cnt1++ )
{
//logic to compare each array element, starting with lArr[1], with highest
//replace highest if the value in lArr[cnt] is higher than the value in variable highest
if(highest<lArr[cnt1])
highest = lArr[cnt1];
}
//print the highes value
cout<<"Highest is "<<highest<<endl;
//generate random number in 2d array between 1 to 53
for ( cnt1 = 0 ; cnt1 < 3 ; cnt1++ )
{
for ( cnt2 = 0 ; cnt2 < 5; cnt2++ ){
iArr[cnt1][cnt2] = rand()%(53) + 1;
}
}
cout<<endl;
//print the 2 d array with setw(3)
cout<<"iArr is "<<endl;
for ( cnt1 = 0 ; cnt1 < 3 ; cnt1++ )
{
for ( cnt2 = 0 ; cnt2 < 5; cnt2++ ){
cout<<setw(3)<<iArr[cnt1][cnt2];
}
cout<<endl;
}
cout<<endl;
//print 2d array iArry column wise
cout<<"iArry print in column wise "<<endl;
for ( cnt1 = 0 ; cnt1 < 5 ; cnt1++ )
{
for ( cnt2 = 0 ; cnt2 < 3; cnt2++ ){
cout<<setw(3)<<iArr[cnt2][cnt1];
}
cout<<endl;
}
cout<<endl;
//ask user to eneter name
cout<<"Enter the name "<<endl;
cin.getline(sName,30);
//print the name
cout<<sName<<endl;
cnt1 = 0;
//print the Ascci value of name
while(sName[cnt1]!='\0'){
cout<<sName[cnt1]<<" Ascci is "<<int(sName[cnt1])<<endl;
cnt1++;
}
//copy Albert Einstein to sName array
strcpy(sName,"Albert Einstein");
cout<<sName<<endl;
//print the ascii of 12th character
cout<<"12th character ascii value is "<<int(sName[12]);
  
return 0;
}

Screenshots -

pls do give a like thanks


Related Solutions

C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using namespace std; const int CWIDTH = 26; int main() {    int choice;    double convertFoC, converCtoF;    double starting, endvalue, incrementvalue;    const int CWIDTH = 13;    do {       cin >> choice;    switch (choice)    {        cin >> starting;    if (starting == 28){       cout << "Invalid range. Try again.";    }    while (!(cin >> starting)){       string  garbage;       cin.clear();       getline(cin, garbage);       cout << "Invalid data Type, must be a number....
Add Bubble Sorting & Binary Search Functions to the following code (C++) #include<iostream> #include<fstream> #include<string> #include<iomanip>...
Add Bubble Sorting & Binary Search Functions to the following code (C++) #include<iostream> #include<fstream> #include<string> #include<iomanip> #include<algorithm> using namespace std; const int row = 10; const int col = 7;   ifstream name; ifstream grade; ofstream out; void readData(string stname[row], int stgrade[row][col]); void stsum(int stgrade[row][col], int total[row]); void staverage(int total[row], double average[row]); void stlettergrade(double average[row],char ltrgrade[row]); void displayData(string stname[row], int stgrade[row][col], int total[row], double staverage[row], char ltrgrade[row]); void swapltrgrade(char* xp, char* yp); int main() {    int STG[row][col] = { {0},{0}...
#include <iostream> #include <string> #include <iomanip> #include <fstream> using namespace std; struct Product {    string...
#include <iostream> #include <string> #include <iomanip> #include <fstream> using namespace std; struct Product {    string itemname;    int id;    string itemcolor;    double cost; }; void fillProduct(Product[10], int& );//read data from a file and store in an array void writeProduct(Product[10], int);//write the array into a file void writeBinary(Product[10], int);//write the array into a file in binary mode void printbinary(Product[10], int);//read data from the binary file and print int main() {    Product myList[10];    int numItems = 0;...
#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact {...
#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact { public: Contact(string init_name = "", string init_phone = "000-000-0000"); void setName(string name); void setPhone(string phone); string getName()const; string getPhone()const; friend ostream& operator << (ostream& os, const Contact& c); friend bool operator == (const Contact& c1, const Contact& c2); friend bool operator != (const Contact& c1, const Contact& c2); private: string name, phone; }; Contact::Contact(string init_name, string init_phone) { name = init_name; phone = init_phone;...
Complete the following program #include<iostream> #include<iomanip> #include<fstream> using namespace std; int main() { // I -...
Complete the following program #include<iostream> #include<iomanip> #include<fstream> using namespace std; int main() { // I - Declaring a five by five array /* II - Read data from data.txt and use them to create the matrix in the previous step*/    // III - Count and print the number of even integers in the matrix. /* IV - Calculate and print the sum of all integers in the columns with an even index value. Please note the column index begins...
What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std;...
What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std; float series(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum += r[i];    return sum; } float parallel(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum = sum + (1 / r[i]);...
Read Ch. 3. Using the C++ editor, type the following program: #include <iostream> #include <string> using...
Read Ch. 3. Using the C++ editor, type the following program: #include <iostream> #include <string> using namespace std; int main () {        //declarations string name;        //input cout <<"Enter your first name" << endl;        cin >> name;        //output        cout << "Hello " << name << "! " << endl;        return 0; } Compile and run. What does the cin statement do? What does the cout statement do? Is name a variable or a constant? What...
this is cahce memory related c program....and not working or running properly??????? #include <iostream> #include <string>...
this is cahce memory related c program....and not working or running properly??????? #include <iostream> #include <string> #include <vector> #include <iomanip> #include <cmath> #include <cstdlib> #include <ctime> using namespace std; int cash_type, block_size, cash_size,number_of_blocks=0;; int compulsry_misses=0, capcity_misses=0, conflict_misses=0; #define DRAM_SIZE (64*1024*1024) unsigned int m_w = 0xABABAB55; /* must not be zero, nor 0x464fffff */ unsigned int m_z = 0x05080902; /* must not be zero, nor 0x9068ffff */ unsigned int rand_() { m_z = 36969 * (m_z & 65535) + (m_z >>...
Complete the following TODO: parts of the code in C++ #include <iostream> #include <string> #include <limits>...
Complete the following TODO: parts of the code in C++ #include <iostream> #include <string> #include <limits> #include <vector> using namespace std; // // CLASS: NODE // class Node{ public: int value = 0; // our node holds an integer Node *next = nullptr; // our node has a pointer to the next Node Node(int i){ // contructor for our Node class value = i; // store a copy of argument "i" in "value" next = nullptr; // be sure next...
complete the program #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; int main(int argc, char**...
complete the program #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; int main(int argc, char** argv) { int number, sum, count; // Write a while loop that reads a number from the user and loop // until the number is divisible by 7 cout << "What is the number? "; cin >> number; while ( ... ) { ... } cout << number << " is divisible by 7!! << endl << endl; // Write a for loop that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT