In: Computer Science
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;
}
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