Question

In: Computer Science

Using the below program as a starting point C++ Program - Arrays- Chapter 9     Include...

Using the below program as a starting point

C++ Program - Arrays- Chapter 9

    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 so I can grade more easily.
C++ Program Pointers

Before starting on this project you should make sure points 1 to 15 are working properly from Chapter 7 Arrays project.
This program is to have no programer created functions. Just do everything in main and make sure you comment each step so I can grade more easily.
C++ Program Extension - This will be graded as a separate program but will add the following to Chapter 7 Arrays project. Don't forget to include comments with the corresponding number.

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.

USE THIS PROGRAM AS STARTING POINT

//Christopher Cupani
//Sept 22, 2019
//Online class
//Chapter 7
#include <iostream>
#include <istream>
#include <string>
#include <iomanip>
#include <cstring>
using namespace std;

int main()
{
   //The variables are declare
   double dArr[5];
   double lArr[7] = { 100000, 134567, 123456, 9, -234567, -1, 123489 };
   int iArr[3][5];
   char sName[30] = { 'C','h','r','i','s' };
   //define variables cnt1 and cnt2 (short data types)
   short cnt1, cnt2;
   long double total = 0;
   //define one long variable and it call highest
   long highest;
   // 4
   int i;
   //Create a loop to put a random number into each of the elements
   //of the array of double.
   srand(time(0));
   for (i = 0; i < 5; i++) {
       double f = (double)rand() / RAND_MAX;
       dArr[i] = 1 + f * (49);
   }
   //Create a for loop to display all of the values in dArr.
   for (i = 0; i < 5; i++) {
       cout << dArr[i] << " ";
   }
   cout << endl;
   // loop to add up the array of double, dArr, into the
   //variable total
   for (i = 0; i < 5; i++) {
       total += dArr[i];
   }
   //display the total
   cout << "Total of double array is " << total << endl;
   //display the average
   cout << "Average of double array is " << total / 5 << endl;
   // Create a for loop that was like example in the instructions
   //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
       if (highest < lArr[cnt1])
           highest = lArr[cnt1];
   }
   //display 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;
   //Display 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;
   //Display 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;
   //Use cin.getline( ...... ) to add another name into variable sName.
   cout << "Enter the name " << endl;
   cin.getline(sName, 30);
   //Display the name
   cout << sName << endl;
   cnt1 = 0;
   //Display the ascii value of each character in the char array,
   //1 per line. Use a while loop to look for the '\0' as a signal to end.
   while (sName[cnt1] != '\0') {
       cout << sName[cnt1] << " Ascci is " << int(sName[cnt1]) << endl;
       cnt1++;
   }
   //Entering Albert Einstein to sName array and
   //using strcpy_s function.
   strcpy_s(sName, "Albert Einstein");
   cout << sName << endl;
   //Display the ascii of 12th character
   cout << "12th character ascii value is " << int(sName[12]);

   return 0;
}

Solutions

Expert Solution

Screenshot

Program

// Christopher Cupani
//Sept 22, 2019
//Online class
//Chapter 7
#include <iostream>
#include <istream>
#include <string>
#include <iomanip>
#include <cstring>
using namespace std;

int main()
{
   //The variables are declare
   double dArr[5];
   double lArr[7] = { 100000, 134567, 123456, 9, -234567, -1, 123489 };
   int iArr[3][5];
   char sName[30] = { 'C','h','r','i','s' };
   //define variables cnt1 and cnt2 (short data types)
   short cnt1, cnt2;
   long double total = 0;
   //define one long variable and it call highest
   long highest;
   // 4
   int i;
   //Create a loop to put a random number into each of the elements
   //of the array of double.
   srand(time(0));
   for (i = 0; i < 5; i++) {
       double f = (double)rand() / RAND_MAX;
       dArr[i] = 1 + f * (49);
   }
   //Create a for loop to display all of the values in dArr.
   for (i = 0; i < 5; i++) {
       cout << dArr[i] << " ";
   }
   cout << endl;
   // loop to add up the array of double, dArr, into the
   //variable total
   for (i = 0; i < 5; i++) {
       total += dArr[i];
   }
   //display the total
   cout << "Total of double array is " << total << endl;
   //display the average
   cout << "Average of double array is " << total / 5 << endl;
   // Create a for loop that was like example in the instructions
   //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
       if (highest < lArr[cnt1])
           highest = lArr[cnt1];
   }
   //display 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;
   //Display 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;
   //Display 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;
   //Use cin.getline( ...... ) to add another name into variable sName.
   cout << "Enter the name " << endl;
   cin.getline(sName, 30);
   //Display the name
   cout << sName << endl;
   cnt1 = 0;
   //Display the ascii value of each character in the char array,
   //1 per line. Use a while loop to look for the '\0' as a signal to end.
   while (sName[cnt1] != '\0') {
       cout << sName[cnt1] << " Ascci is " << int(sName[cnt1]) << endl;
       cnt1++;
   }
   //Entering Albert Einstein to sName array and
   //using strcpy_s function.
   strcpy_s(sName, "Albert Einstein");
   cout << sName << endl;
   //Display the ascii of 12th character
   cout << "12th character ascii value is " << int(sName[12]) << endl;
   //Define a pointer to a double, pdArray
   double *pdArray;
   //Assign the pointer, pdArray, to contain the address of the double array, dArr:
   pdArray=dArr;
   // Use the array name, dArr, to print out the array elements with subscript notation, [ ].
   //All on 1 line a space between each.
   cout << "\nDisplay double array using subscript notation:-\n";
   for (i = 0; i < 5; i++) {
       cout << dArr[i] << " ";
   }
   cout << endl;
   // 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.
   cout << "\nDisplay double array using pointer:-\n";
   for (cnt1 = 0; cnt1 < 5; cnt1++) {
       cout << *(pdArray + cnt1) << " ";
   }
   cout << endl;
   //Use the array name for the double array and pointer notation
   //to print the entire array, all on one line.
   cout << "\nDisplay double array using pointer notation:-\n";
   for (cnt1 = 0; cnt1 < 5; cnt1++) {
       cout << *(dArr + cnt1) << " ";
   }
   cout << endl;
   //Using a different pointer, piArray,
   //allocate enough memory for 100 int's
   //and assign the address to the pointer.
   int* piArray;
   piArray = new int[100];
   //In a for loop assign every item in the array to be a random number from 1 to 49
   for (int i = 0; i < 100; i++) {
       *(piArray + i) = rand() % 49 + 1;
   }
   //Using cout print the first 10 items in the array, all on 1 line.
   cout << "\nDisplay 10 elements of an int pointer array:-\n";
   for (int i = 0; i < 10; i++) {
       cout << *(piArray + i) << " ";
   }
   cout << endl;
   return 0;
}

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

Output

18.6682 17.0756 43.9002 16.9919 43.4187
Total of double array is 140.055
Average of double array is 28.0109
Highest is 134567

iArr is
51 35 40 30 17
47 36 6 30 41
36 44 26 23 4

iArry print in column wise
51 47 36
35 36 44
40 6 26
30 30 23
17 41 4

Enter the name
Michael
Michael
M Ascci is 77
i Ascci is 105
c Ascci is 99
h Ascci is 104
a Ascci is 97
e Ascci is 101
l Ascci is 108
Albert Einstein
12th character ascii value is 101

Display double array using subscript notation:-
18.6682 17.0756 43.9002 16.9919 43.4187

Display double array using pointer:-
18.6682 17.0756 43.9002 16.9919 43.4187

Display double array using pointer notation:-
18.6682 17.0756 43.9002 16.9919 43.4187

Display 10 elements of an int pointer array:-
16 39 16 43 34 22 7 39 49 47


Related Solutions

C++ PROGRAM Programming Exercise 11 in Chapter 8explains how to add large integers using arrays. However,...
C++ PROGRAM Programming Exercise 11 in Chapter 8explains how to add large integers using arrays. However, in that exercise, the program could add only integers of, at most, 20 digits. This chapter explains how to work with dynamic integers. Design a class named largeIntegers such that an object of this class can store an integer of any number of digits. Add operations to add, subtract, multiply, and compare integers stored in two objects. Also add constructors to properly initialize objects...
Starting with the following C++ program: #include <iostream> using namespace std; void main () { unsigned...
Starting with the following C++ program: #include <iostream> using namespace std; void main () { unsigned char c1; unsigned char c2; unsigned char c3; unsigned char c4; unsigned long i1 (0xaabbccee); _asm { } cout.flags (ios::hex); cout << "results are " << (unsigned int) c1 << ", " << (unsigned int) c2 << ", " << (unsigned int) c3 << ", " << (unsigned int) c4 << endl; } Inside the block denoted by the _asm keyword, add code to...
Need this C++ code to be modified to work in C, still using 2d arrays... #include...
Need this C++ code to be modified to work in C, still using 2d arrays... #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; //Implementation of main function int main() { srand(time(NULL)); //Declaration of output,result,i,j,k ,figure as integer type and //assign flog with 0 int output[5][5], result[5], i, j, k, figure = 0; //Display statement cout << "The classic BINGO cards contains 25 squares arranged in five vertical" << endl; cout << "columns and five side to side rows. Each...
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...
Write a program of Binary Search in C++ by using function and arrays with the explanation.
Write a program of Binary Search in C++ by using function and arrays with the explanation.
Write a C++ program based on the cpp file below ++++++++++++++++++++++++++++++++++++ #include using namespace std; //...
Write a C++ program based on the cpp file below ++++++++++++++++++++++++++++++++++++ #include using namespace std; // PLEASE PUT YOUR FUNCTIONS BELOW THIS LINE // END OF FUNCTIONS void printArray(int array[], int count) {    cout << endl << "--------------------" << endl;    for(int i=1; i<=count; i++)    {        if(i % 3 == 0)        cout << " " << array[i-1] << endl;        else        cout << " " << array[i-1];    }    cout...
Take the following C++ program and translate it into Pep/9 assembly language #include using namespace std;...
Take the following C++ program and translate it into Pep/9 assembly language #include using namespace std; int age; char first, last; int main() {    cin >> age;    cin >> first >> last;    cout << "Your age " << age << endl;    cout << "Initials " << first << last << endl;    if (age >= 30)        cout << “Cannot trust\n”;    return 0; }
Take the following C++ program and translate it into PEP/9 assembly language #include <iostream> using namespace...
Take the following C++ program and translate it into PEP/9 assembly language #include <iostream> using namespace std; int num; char letter; int main() {    cin >> num;    cin >> letter;    cout << "You inputted " << num << endl;    cout << "Option " << letter << endl;    if (letter == '*')       cout << "Multiplied by 2 " << num*2 << endl;    return 0; }
Take the following program from C++ and translate it into Pep/9 assembly language: #include <iostream> using...
Take the following program from C++ and translate it into Pep/9 assembly language: #include <iostream> using namespace std; int theArray[] = { 5, 11, -29, 45, 9, -1}; void sumPos(int ary[], int len, int &sum) {    sum = 0;    for (int i = 0; i < len; i++)            if (ary[i] > 0)                sum = sum + ary[i]; } int main() {    int total;    sumPos(theArray, 6, total);    for (int k=0; k < 6; k++)...
C++ Develop program in C++ using arrays of characters, subscript operator, the cstring library, and functions...
C++ Develop program in C++ using arrays of characters, subscript operator, the cstring library, and functions with arguments. Create programs with small functions where main goes to a series of functions where the real work takes place. Don’t use global variables and don’t use break within a loop (unless working with a switch statement). Functions can’t have more than 30 statements of code, not including comments, blank lines, or variable definitions. Don’t use a return in the middle of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT