Page 1 of 1

I always give a positive rating! Language is C++ I know this looks really long, but this is actually very easy! Just fil

Posted: Sun May 15, 2022 2:07 pm
by answerhappygod
I always give a positive rating!
Language is C++
I know this looks really long, but this is actually very
easy! Just fill in the blanks(_____) in the code I
have pasted here:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
void readInts(string, int*, int *);
void readIntArray(ifstream &inStream, int iarr[], int l);
void printIntArray(int *, int);
void swapFirstLast(int [], int);
void swapFirstSecond(int []);
void swapInts(int*, int*);
int main()
{
// declare an int const LEN = 10
;
// declare 2 double pointers dptr1 and dptr2
;
// declare 2 double variables dnum1, and dnum2, initializing
// them to 2.5 and 100.1
;
// declare a double array called darr of length LEN,
initializing
// to 1.5 2.2 3.7 4.5 5 and LEN. The rest will equal 0.
;
// declare 2 int pointers iptr1 and iptr2
;
// declare 2 int variables inum1 and inum2
;
// declare an int array called iarr of length LEN,
initializing
// to 42 23 43 71 and 101. The rest will equal 0.
;
string filename;
ifstream inStream;
// to dptr1 assign the address of dnum1
;
cout << fixed << setprecision(4);
// print the value of dptr1 as a long (by casting it to a
long)
// and in hexadecimal (by just printing the variable)
cout << "doubles " << __________ << "(long) =
"
<< __________ << "(hex)\n";
// print the addresses of dptr1 and dptr2
cout << "dptr addresses [" << (long)___________
<< "] ["
<< ______________ << "]\n";
// print the addresses of dnum1 and dnum2
cout << "dnum addresses [" << ___________ << "]
["
<< _____________ << "]\n";
// print the address of the beginning of darr
cout << "darr address [" << ____________ <<
"]\n\n";
// print the addresses of iptr1 and iptr2
cout << "ints\n" << "int ptrs [" <<
______________
<< "] [" << ____________ << "]\n";
// print the addresses of inum1 and inum2
cout << "inum addresses [" << ___________ << "]
["
<< _____________ << "]\n";
// print the address of the beginning of iarr
cout << "iarr address [" << ____________ <<
"]\n\n";
cout << "Values of the double variables: ";
// print dnum1 and dnum2
cout << dnum1 << " " << dnum2 <<
endl;
// assign dptr1 the address of dnum1
;
// make dptr2 point to the same thing as dptr1
;
// use * of dptr2 to set the VALUE of dnum1 to 3.14159
____________ = 3.14159;
cout << " Values of double variables again: ";
// print dnum1 and dnum2 again
cout << dnum1 << " " << dnum2 <<
endl;
// print the values of what dptr1 and dptr2 point at
cout << "value pointed at by dptr1 & dptr2: "
<< ___________ << " " << __________ <<
endl;
cout << "int array:\n";
// set iptr1 equal to the beginning address of iarr
;
for (int i=0; i < LEN ; i++)
{
// use iptr1 to print each of the 10 numbers in the
// int array and increment the pointer
cout << " " << ______________ << endl;
}
cout << "==============\n\n";
cout << "double array:\n";
// set dptr1 equal to the beginning address of darr
;
for (int i=0; i < LEN ; i++)
{
// use dptr1 to print each of the 10 numbers in the
// double array and increment the pointer
cout << " " << __________ << endl;
}
// set iptr2 to the address of inum2
;
// call void readInts( string promptString, int* int1, int*
int2)
// - read values for inum1 and use iptr2 to read a value for
inum2
readInts("Enter 2 ints to return: ", &inum1, iptr2);
cout << "ints read by readInts(): " << inum1 <<
", "
// 2 different values can fill in this blank. You need to know
both.
<< ______________ << endl << endl;
cout << "enter a file name to read from: ";
cin >> filename;
inStream.open(filename.c_str());
if ( !inStream )
{
cout << "Error!\n";
return 1;
}
// call void readIntArray(ifstream infile, int intArray[], int
len)
;
cout << "PRINT int ARRAY 1:\n";
// call void printIntArray(int arr[], LEN)
;
cout << "swap first and last elements in the array\n";
// call void swapFirstLast(int arrINTs[], LEN)
;
cout << "PRINT int ARRAY 2:\n";
// call void printIntArray(int arr[], LEN)
;
cout << "swap first and second elements of the
array\n";
// call void swapFirstSecond(int arrints[])
;
cout << "PRINT int ARRAY 3:\n";
// call void printIntArray(int arr[], LEN)
;
cout << "Point iptr1 to second and iptr2 to "
<< "third numbers in the iarr\n";
;
;
cout << "Swap ints pointed at by iptr1 and 2.\n";
// call void swapInts(int *p1, int *p2)
;
cout << "PRINT int ARRAY 4:\n";
// call void printIntArray(int arr[], LEN)
;
return 0;
}
void readInts(_____________________________)
{
cout << prompt ;
cin >> ____________ >> __________;
return;
}
void readIntArray(_______________________)
{
for (int i = 0; i < l ; i++)
{
in >> ______________;
}
return;
}
void printIntArray(_____________________)
{
int i;
for ( i = 0; i < len-1 ; i++)
{
cout << __________________ << ", ";
}
cout << _________________ << endl << endl;
return;
}
void swapFirstLast(____________________)
{
int __________________;
;
;
return;
}
void swapFirstSecond(__________________)
{
int _______________;
;
;
return;
}
void swapInts(____________________)
{
int _____________;
;
;
return;
}
Just follow the steps in the document below and paste
your code in the answer.
Thanks!
I Always Give A Positive Rating Language Is C I Know This Looks Really Long But This Is Actually Very Easy Just Fil 1
I Always Give A Positive Rating Language Is C I Know This Looks Really Long But This Is Actually Very Easy Just Fil 1 (130.51 KiB) Viewed 52 times
In Class 11 Exercise - Pointers and Variable Addresses Assignment Overview Pointers are variables that contain the memory address of data, i.e. typically of other variables, elements of arrays, or dynamically allocated memory. They involve indirect access to data and require the programmer to think in terms of a "double hop to data, i.e. data values "in" variables. Pointers add a new level of abstraction and difficulty to programming. Most programming languages do not provide such low-level access to data. This is one of the main aspects that make CH and C such powerful languages. Unfortunately, neither the language nor compilers do any checking on values used as addresses. Making sure that pointers have valid values is the sole responsibility of programmers, similar to programmers being responsible for array indices staying in bounds. As one student found out with the last Homework, if you access memory not inside your program accidentally, then your virus protection software may block your program from running! You can accidentally do this by using an index past the end of an array, or mis- setting a pointer variable and trying to set the memory that address to a value or just using that value in a calculation Goals This assignment has you l) output the addresses of variables and the values of pointers (addresscs), 2) use pointers to access clements of arrays, and 3) use and write functions that have parameters of arrays and pointers and addresses of variables. I hope that you think about what you are doing to accomplish these things to get the proper output and that you really consider what the output means. If you don't look at the addresses in the output, you aren't understanding the material, Most professional programmers consider pointers one of the most difficult topic in programming. I highly recommend you attend class this week. Complete the Code Copy and paste the following code into CodeBlocks and Fill in the Blanks: • Fill in the places where there is a ; semicolon by itself, i.e. you need to put in a statement where there is a null statement. • Fill in places with incomplete lines, i.e. with blanks #include <iostream> #include <iomanip> #include <istream> using namespace sto;

void readInts(string, int, int *); void readIntArray (ifstream &instream, int iarr[], int i); void prinLInLArray(int *, inl); void swapFirstLast(int [., ins): void swapFirsLSecond(int (); void swapInts (Ant*, int); in: rain() i declare an in const LEN - 10 i // declare 2 double pointers dotrl and dptr2 7 // declare 2 double variables drum', and dnun2, initializing Lhem to 2.5 and 100.1 // declare a double array called darr o. length LEN, inilializing to 1.5 2.2 3.7 4.5 5 and LEN. The rest will equal 0. ; // declare 2 int pointers iptri and iptr2 // declare 2 int variables irol and inur2 ; // declare an int array called iarr of length LEN, nitializing // to 12 23 13 71 and 101. The rest will equal 0. i string filename; ifsLreat insl reart; // -o dptrl assign the address of dnum1 i cout < fixed « setprecision(4); // print the value of dot-l as a ong (by casting it to a long) // ano in hexadecimal (by jus. printing the variab c) cout << "doubles " << <"(long) = " << " (hex) \n"; // print the addresses of cptrl and cp=r2 coul << "dole addresses " << (long) << "\n"; // print the addresses of anuml and onwr2 cout << "dnum addresses [" << [" << "\n"; // print the address of the beginning of darr cout << "dary address [" << << "\n\n"; // print the adoressos of iptrl and iptr2

cout << "ints." << "int ptrs << "][" < << "\n"; // print the addresses of inurl and inum2 cout << "inum addresses [" << [" <<"\n"; // print the address of the beginning of iarr cout << "iary address [" << << "\n\n"; cout << "values of the double variables: "; // print dnum ano dnum2 cout << dnuml << " << dnum < endl; // assign dotr: the address of dnuml ; // make dptr2 point to the same thing as aptr1 ; // Use + of dpt 2 to set the VALJE of anum 1 to 3.14:59 = 3.14-59; cout << Values of double variables again: "; // print dnuml aná dnum2 agair cout << dnum << dnum2 << endl; // print the values of what dptrl and dpt-2 point at cout << "value pointed at oy dptrl & dotr2: " << endl; cout << "int array: \n"; // sel ipurl equal to the beginning address of iarr for (int i=0; i < LEN ; i++) // use iptrl to print each of the 10 numbers in the int array ard incremer the pointer cout << " << endl; } cout << " =\n\n"; cout << "double array: \n"; // set dptrl equal to the beginning address of darr 1 for (int i=0; i < LEN ; i++) f // use dptri to print each of the 10 numbers in the doub-e array and Encrement the pointer cout << " < endl; ? // set iptr2 to the address of inum2

// call void readIn:si string promptstring, int* inti, int* int2) read values for inuml and use iolr2 Lo read a value for in...2 reacInts ("Enter 2 ints to return: ", &inuml, ptr2); coul < "inls read by readInLs): << inurl <<", " // 2 different values can fill in this blank. You need to know both. << end << endl; cout << "enter a file name to read from: "; cin >> filename; instream.opon (filename.c_str()); if i !instream) 1 cout << "Error!\n"; relurn 1; 1 // call void readIn-Array(ifs-rean infile, int intArray[], int len) cout << "PRINT int ARRAY 1:\n"; // call void printIntArray(in arri], LEN! i cout < "swap first and last elemen:s in the array\n"; // call void swapFirst Last (in: arrINTS[], LEN) i coul << "PRINT inL ARRAY 2:\n"; // call void printintArray in: arr.), LEN) ; cout << "swap first and second elements of the array\n"; // call void swapFirstSecond(int arrintsil cout << "PRINT int ARRAY 3:\n"; // call void printi tarray(ins arril, LEN) i cout << "Foint iptrl to second and iptr2 to " <<"Lhiro numbers in the lar:\n"; ; cout << "Swap ints pointed at by iperl and 2.\n"; // call void swap In-s(int *pi, int *p2) cout << "PRINT int ARRAY 4:\n"; // call void printi tarray in arr , LEN) i

return 0; void readlnl si cout << prompt ; cin >> return; ; : void readlntArray ( for (int i = 0; i <:; i++) 1 in >> 1 return; void print.IntArzay! int i; for i i = 0; i < len-1 ; i++) cout << ? cout << return; << endl << endl; : void swapFirstlasti int i return; : void swapFirstSecond ; int ; ; return;

void swapints in ; ; return; Output

doubles 7274152 (long) = 0x6efea8 (hex) dptr addresses (7274164] [7274160] dnum addresses [7274152] [7274144] darr address [7274064] ints int ptrs [7274060] [7274056] inum addresses [7274052] [7274048] iarr address [7274008] Values of the double variables: 2.5000 100.1000 Values of double variables again: 3.1416 100.1000 value pointed at by dptrl & dptr2: 3.1416 3.1416 int array: 42 23 43 71 101 0 0 0 0 double array: 1.5000 2.2000 3.7000 4.5000 5.0000 10.0000 0.0000 0.0000 0.0000 0.0000 Enter and 2 ints: 34 22 ints read by readInts(): 34, 22 enter a file name to read from: in PRINT int ARRAY 1: 2, 4, 6, 8, 11, 13, 15, 17, 19, 1 swap first and last elements in the array PRINT int ARRAY 2 : 1, 4, 6, 8, 11, 13, 15, 17, 19, 2

swap first and second elements of the array PRINT int ARRAY 3: 4, 1, 6, 8, 11, 13, 15, 17, 19, 2 Point iptrl to second and iptr2 to third numbers in the iarr Swap ints pointed at by iptri and 2. PRINT int ARRAY 4: 4, 6, 1, 8, 11, 13, 15, 17, 19, 2 Input File 2 4 6 8 11 13 15 17 ا ي ا . ي