Question

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 programer created functions. Just do everything in main and make sure you comment each step.

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

Extend the Array project to include:
16. Define a pointer to a double, pdArray.
17. Assign the pointer, pdArray, to contain the address of the double array, dArr:
18. Use the array name, dArr, to print out the array elements with subscript notation, [ ]. All on 1 line a space between each.
19. Use the pointer to print out the array elements with pointer notation while not changing the pointer itself. Use a for loop. *( pdArray + Cnt1) would be an example. All on 1 line a space between each.
20. Use the pointer to print out the array elements with pointer notation but change the pointer to point to the actual array element rather than the method in 18. All on 1 line.
*pdArray would do this if the loop has the following post loop operation:   pdArray++
21. Use the array name for the double array and pointer notation to print the entire array, all on one line.
22. Using a different pointer, piArray, allocate enough memory for 100 int's and assign the address to the pointer.
23. In a for loop assign every item in the array to be a random number from 1 to 49 ( hint: rand() % 6 + 1 gives random numbers from 1 to 6 )
24. Using cout print the first 10 items in the array, all on 1 line.

0 0
Add a comment Improve this question Transcribed image text
Answer #2

Here is the code with output for the question. Please post a comment in case of any doubts, I shall respond. Also if the answer helped, request you to rate it . Thank you very much.

Note:

1. In step 1, please put in your name for sName

2. I have used cout << fixed << setprecision(2) for formatting purposes. These need iomanip which is already included. In case you don't need them , you can remove that line on line 31. You can check the difference in output by commenting out that line.

3. In step 14, I have used strcpy() since my system does not have the library function strcpy_s(). You can change it to strcpy_s(sName, sizeof(sName), "Albert Einstein"); and check if that works for you on your system.

#include <iostream>

#include <cstdlib>

#include <ctime>

#include <iomanip>

#include <cstring>

using namespace std;

int main()

{

//step1

double dArr[5];

long lArr[7] = {100000, 134567, 123456, 9, -234567, -1, 123489};

int iArr[3][5];

char sName[31] = "myname"; //30 typable characters + 1 for \0

  

//step2

int cnt1, cnt2;

long double total = 0;

  

//step3

long highest;

  

//step4 : put random variables in dArr using rand()

srand(10);

for(cnt1 = 0; cnt1 < 5; cnt1++)

dArr[cnt1] = rand();

  

//print the contents of dArr

  

cout << fixed << setprecision(2); //used to display in non-scientific format and with 2 decimal places

  

cout << "dArr: " ;

for(cnt1 = 0; cnt1 < 5; cnt1++)

cout << " " << dArr[cnt1];

  

cout << endl;

  

  

//step5: loop to add to total

for(cnt1 = 0; cnt1 < 5; cnt1++)

total += dArr[cnt1];

  

//step 6:

cout << "\ntotal = " << total << endl;

cout << "average = " << total / 5 << endl;

  

//step 7:

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( lArr[cnt1] > highest)

highest = lArr[cnt1];

}

  

//step 8

cout << "highest = " << highest << endl;

  

//step 9 initialize 3x5 array

srand(time(NULL));

for ( cnt1 = 0 ; cnt1 < 3 ; cnt1++ ) //for each row

{

for(cnt2 = 0; cnt2 < 5; cnt2++ ) //for each col

{

iArr[cnt1][cnt2] = rand() % 53 + 1; //generate a random number from 1- 53

}

}

  

  

  

//step10 display the 2D array

cout << "row wise 2D array" << endl;

for ( cnt1 = 0 ; cnt1 < 3 ; cnt1++ ) //for each row

{

cout << endl;

for(cnt2 = 0; cnt2 < 5; cnt2++ ) //for each col

{

cout << setw(4) << iArr[cnt1][cnt2] ;

}

}

  

cout << endl;

  

//step11 display the 2D array columnwise

cout << "col wise 2D array" << endl;

for ( cnt1 = 0 ; cnt1 < 5 ; cnt1++ ) //for each col

{

cout << endl;

for(cnt2 = 0; cnt2 < 3; cnt2++ ) //for each row

{

cout << setw(4) << iArr[cnt2][cnt1] ;

}

}

  

cout << endl;

  

//step 12 get another name

cout << "Enter a name: ";

cin.getline(sName, 30);

  

//step 13 print ascii value one per line

cout << "ascii values of " << sName << endl;

cnt1 = 0;

while(sName[cnt1] != '\0')

{

cout << (int)sName[cnt1] << endl; //cast to int since we want ascii value of the character

cnt1++;

}

  

//step 14 copy Albert Einstein to sName

strcpy(sName,"Albert Einstein");

  

//step 15 print 12th char's ascii value

cout << "12th char's ascii in Albert Einstein is " << (int) sName[11] << endl; //12 char is at index 11

  

//step 16 define pdArray

double *pdArray;

  

//step 17 assign address of dArr to pdArray

pdArray = dArr;

  

  

//step 18 print the contents of dArr

cout << "dArr: " ;

for(cnt1 = 0; cnt1 < 5; cnt1++)

cout << " " << dArr[cnt1];

cout << endl;

  

  

//step 19 print using pdArray without moving pointer

cout << "using pointer notation *(pdArray + cnt1): " ;

for(cnt1 = 0; cnt1 < 5; cnt1++)

cout << " " << *(pdArray + cnt1);

cout << endl;

  

//step 20 print using pdArray, moving pointer

cout << "using pdArray++ : " ;

  

for(cnt1 = 0; cnt1 < 5; cnt1++, pdArray++) //move the pointer, cnt1 is used to track how many times to move the pointer

cout << " " << *pdArray;

cout << endl;

  

  

//step 21 print using array name and pointer notation

cout << "using *(dArr + cnt1): " ;

  

for(cnt1 = 0; cnt1 < 5; cnt1++ )

cout << " " << *(dArr + cnt1);

cout << endl;

  

//step 22 allocate 100 ints and assign to pointer

int *piArray = new int[100];

  

//step23 assign random number to elements , range is 1-49

for(cnt1 = 0; cnt1 < 100; cnt1++)

*(piArray + cnt1) = rand() % 49 + 1; // it can also written as piArray[cnt1] = rand() % 49 + 1;

  

//step 24 print out the first 10 elements in a line

cout << "display the first 10 ints : " ;

  

for(cnt1 = 0; cnt1 < 10 ; cnt1++)

cout << " " << piArray[cnt1];

cout << endl;

  

return 0;

}

output

dArr: 168070.00 677268843.00 1194115201.00 1259501992.00 703671065.00

total = 3834725171.00
average = 766945034.20
highest = 134567
row wise 2D array

45 7 21 37 49
16 23 45 46 42
13 18 15 1 46
col wise 2D array

45 16 13
7 23 18
21 45 15
37 46 1
49 42 46
Enter a name: john
ascii values of john
106
111
104
110
12th char's ascii in Albert Einstein is 116
dArr: 168070.00 677268843.00 1194115201.00 1259501992.00 703671065.00
using pointer notation *(pdArray + cnt1): 168070.00 677268843.00 1194115201.00 1259501992.00 703671065.00
using pdArray++ : 168070.00 677268843.00 1194115201.00 1259501992.00 703671065.00
using *(dArr + cnt1): 168070.00 677268843.00 1194115201.00 1259501992.00 703671065.00
display the first 10 ints : 20 33 49 12 37 36 24 35 19 6

Add a comment
Know the answer?
Add Answer to:
C++ Program - Arrays- Include the following header files in your program:     string, iomanip, iostream Suggestion:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #include <iostream> #include <iomanip> #include <vector> using namespace std; Part 1. [30 points] In this part,...

    #include <iostream> #include <iomanip> #include <vector> using namespace std; Part 1. [30 points] In this part, your program loads a vending machine serving cold drinks. You start with many foods, some are drinks. Your code loads a vending machine from foods, or, it uses water as a default drink. Create class Drink, make an array of drinks, load it and display it. Part 1 steps: [5 points] Create a class called Drink that contains information about a single drink. Provide...

  • #include <iostream> #include <iomanip> #include <vector> #include <string> using namespace std; struct menuItemType { string menuItem;...

    #include <iostream> #include <iomanip> #include <vector> #include <string> using namespace std; struct menuItemType { string menuItem; double menuPrice; }; void getData(menuItemType menuList[]); void showMenu(menuItemType menuList[], int x); void printCheck(menuItemType menuList[], int menuOrder[], int x); int main() { const int menuItems = 8; menuItemType menuList[menuItems]; int menuOrder[menuItems] = {0}; int orderChoice = 0; bool ordering = true; int count = 0; getData(menuList); showMenu(menuList, menuItems); while(ordering) { cout << "Enter the number for the item you would\n" << "like to order, or...

  • in c++ please program for this code #include <iostream> #include <fstream> #include <string> #include <cstring> //...

    in c++ please program for this code #include <iostream> #include <fstream> #include <string> #include <cstring> // for string tokenizer and c-style string processing #include <algorithm> // max function #include <stdlib.h> #include <time.h> using namespace std; // Extend the code here as needed class BTNode{ private: int nodeid; int data; int levelNum; BTNode* leftChildPtr; BTNode* rightChildPtr; public: BTNode(){} void setNodeId(int id){ nodeid = id; } int getNodeId(){ return nodeid; } void setData(int d){ data = d; } int getData(){ return data;...

  • Write a simple Java program with the following naming structure: Open Eclipse Create a workspace called...

    Write a simple Java program with the following naming structure: Open Eclipse Create a workspace called hw1 Create a project called hw1 (make sure you select the “Use project folder as root for sources and class files”) Create a class called Hw1 in the hw1 package (make sure you check the box that auto creates the main method). Add a comment to the main that includes your name Write code that demonstrates the use of each of the following basic...

  • IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are...

    IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are developing a program that works with arrays of integers, and you find that you frequently need to duplicate the arrays. Rather than rewriting the array-duplicating code each time you need it, you decide to write a function that accepts an array and its size as arguments. Creates a new array that is a copy of the argument array, and returns a pointer to the...

  • Write a C++ program that simulates a lottery game. Your program should use functions and arrays....

    Write a C++ program that simulates a lottery game. Your program should use functions and arrays. Define two global constants: - ARRAY_SIZE that stores the number of drawn numbers (for example 5) -MAX_RANGE that stores the highest value of the numbers ( for example 9 ) The program will use an array of five integers named lottery, and should generate a random number in the range of 0 through 9 for each element of the array. The user should enter...

  • IN C++ PLEASE -------Add code to sort the bowlers. You have to sort their parallel data...

    IN C++ PLEASE -------Add code to sort the bowlers. You have to sort their parallel data also. Print the sorted bowlers and all their info . You can use a bubble sort or a shell sort. Make sure to adjust your code depending on whether or not you put data starting in row zero or row one. Sort by Average across, lowest to highest. The highest average should then be on the last row.. When you sort the average, you...

  • my program wont run on my computer and im not understanding why. please help. #include<iostream> #include<iomanip>...

    my program wont run on my computer and im not understanding why. please help. #include<iostream> #include<iomanip> #include<string> #include "bookdata.h" #include "bookinfo.h" #include "invmenu.h" #include "reports.h" #include "cashier.h" using namespace std; const int ARRAYNUM = 20; BookData bookdata[ARRAYNUM]; int main () { bool userChoice = false; while(userChoice == false) { int userInput = 0; bool trueFalse = false; cout << "\n" << setw(45) << "Serendipity Booksellers\n" << setw(39) << "Main Menu\n\n" << "1.Cashier Module\n" << "2.Inventory Database Module\n" << "3.Report Module\n"...

  • Please include function in this program Write a C program that will calculate the gross pay...

    Please include function in this program Write a C program that will calculate the gross pay of a set of employees utilizing pointers instead of array references. You program should continue to use all the features from past homeworks (functions, structures, constants, strings). The program should prompt the user to enter the number of hours each employee worked. When prompted, key in the hours shown below. The program determines the overtime hours (anything over 40 hours), the gross pay and...

  • Write a program that dynamically allocates an array in the freestore large enough to hold a...

    Write a program that dynamically allocates an array in the freestore large enough to hold a user defined number of test scores as doubles. Once all the scores are entered, the array should be passed to a function that finds the highest score. Another function the array should be passed to a function that finds the lowest score. Another function should be called that calculates the average score. The program should show the highest, lowest and average scores. Must use...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT