Question

In: Computer Science

C++ Download the attached program and complete the functions. (Refer to comments) main.cpp ~ #include #include...

C++

Download the attached program and complete the functions. (Refer to comments)

main.cpp ~

#include 
#include 

#define END_OF_LIST -999

using namespace std;

/*
 * 
 */

int exercise_1() {
    int x = 100; 
    int *ptr; 
    
    // Assign the pointer variable, ptr, to the address of x. Then print out 
    // the 'value' of x and the 'address' of x.    (See Program 10-2) 
    
    return 0; 
}

int exercise_2() {
    int x = 100; 
    int *ptr; 
    
    // Assign ptr to the address of x. Then use indirection to print out the 
    // value of x using pointer variable. (See Program 10-3) 
    
    return 0;
}

int exercise_3() {
    int a[] = {1,2,3,4,5,6,7,8,9};
    int *n; 
    
    // Print the address of the array a, 
    
    // Notice that printing the address of an array is different then printing
    // the address of a variable. 
    
    // Assign the address of the array to the variable n. Then de-reference n 
    // to print out the first, third and fifth elements. (See Programs 10-5 and 
    // 10-6)
 
    return 0;
}

int exercise_4() {

    int a[] = {1,2,3,4,5,6,7,8,9};
    int *n; 
      
    // Assign the address of the array to the variable n. Then use a loop to 
    // print out each element using a while loop (See Program 10-9)
 
    for (int i=0; i<9; i++) {
        cout << "Element " << i << " is " << "... " << endl;
    }
    return 0;
}



int main() {
    exercise_1();
    exercise_2();
    exercise_3();
    exercise_4();
}

Solutions

Expert Solution

#include <iostream>

#define END_OF_LIST -999

using namespace std;

/*
*
*/

int exercise_1() {
int x = 100;
int *ptr;
  
// Assign the pointer variable, ptr, to the address of x. Then print out
// the 'value' of x and the 'address' of x. (See Program 10-2)
ptr = &x;
cout<<"Value of x: "<<x<<"\nAddress of x: "<<&x;
  
return 0;
}

int exercise_2() {
int x = 100;
int *ptr;
  
// Assign ptr to the address of x. Then use indirection to print out the
// value of x using pointer variable. (See Program 10-3)
ptr = &x;
cout<<"\nValue of x using pointer variable: "<<*ptr;
  
return 0;
}

int exercise_3() {
int a[] = {1,2,3,4,5,6,7,8,9};
int *n;
  
// Print the address of the array a,
cout<<"\nAddress of array a: "<<a;
  
// Notice that printing the address of an array is different then printing
// the address of a variable.
  
// Assign the address of the array to the variable n. Then de-reference n
// to print out the first, third and fifth elements. (See Programs 10-5 and
// 10-6)
n= a;
cout<<"\n1st, third, fifth array elements: "<<*n<<","<<*(n+2)<<","<<*(n+4);
return 0;
}

int exercise_4() {

int a[] = {1,2,3,4,5,6,7,8,9};
int *n;
  
// Assign the address of the array to the variable n. Then use a loop to
// print out each element using a while loop (See Program 10-9)
n=a;
for (int i=0; i<9; i++) {
cout << "\nElement " << i << " is " << "... " ;
cout<<*(n+i);
}
return 0;
}

int main() {
exercise_1();
exercise_2();
exercise_3();
exercise_4();
}

output:

Value of x: 100
Address of x: 0x7fff404780b4
Value of x using pointer variable: 100
Address of array a: 0x7fff40478080
1st, third, fifth array elements: 1,3,5
Element 0 is ... 1
Element 1 is ... 2
Element 2 is ... 3
Element 3 is ... 4
Element 4 is ... 5
Element 5 is ... 6
Element 6 is ... 7
Element 7 is ... 8
Element 8 is ... 9

Related Solutions

Lab 11 C++ Download the attached program and run it - as is. It will prompt...
Lab 11 C++ Download the attached program and run it - as is. It will prompt for a Fibonacci number then calculate the result using a recursive algorithm. Enter a number less then 40 unless you want to wait for a very long time. Find a number that takes between 10 and 20 seconds. (10,000 - 20,000 milliseconds). Modify the program to use the static array of values to "remember" previous results. Then when the function is called again with...
Please comments this C++ code and show screenshots of the outputs main.cpp #include<iostream> #include<vector> #include<string> #include"BST.h"...
Please comments this C++ code and show screenshots of the outputs main.cpp #include<iostream> #include<vector> #include<string> #include"BST.h" #include"BST.cpp" using namespace std; std::vector<std::string> tokenize(char line[]) {    std::vector<std::string> tok;        std::string word = "";        for (int i = 0; i < strlen(line); i++)        {            if (i == strlen(line) - 1)            {                word += line[i];                tok.push_back(word);                return tok;       ...
Data Structure using C++ Complete and test the Sudoku problem main.cpp #include <iostream> #include <cmath> #include...
Data Structure using C++ Complete and test the Sudoku problem main.cpp #include <iostream> #include <cmath> #include "sudoku.h" using namespace std; int main() { cout << "See Programming Exercise 18." << endl; } sudoku.cpp #include <iostream> #include "sudoku.h" using namespace std; sudoku::sudoku() { cout << "Write the definition. ." << endl; } sudoku::sudoku(int g[][9]) { cout << "Write the definition." << endl; } void sudoku::initializeSudokuGrid() { cout << "Write the definition." << endl; } void sudoku::initializeSudokuGrid(int g[][9]) { cout << "Write...
For C++ Download the attached program template. The programs calculates the amount of profit or loss...
For C++ Download the attached program template. The programs calculates the amount of profit or loss from a stock transaction. Using the values initialized by the program, calculate and print out the following results: "Total paid for stock" "Purchase commission " "Total received for stock" "Sales commission" "Amount of profit or loss" Use the following formulas: Total_paid = (total purchase price) + purchase_commission; Total_received = (total sales price) - sales_commission; profit = Total_received - Total_paid /* * COSC 1337 Programming...
I need to complete this C++ program. The instructions are in the comments inside the code...
I need to complete this C++ program. The instructions are in the comments inside the code below: ------------------------------------------------------------------------- Original string is: this is a secret! Encypted string is: uijt!jt!b!tfdsfu" Decrypted string is: this is a secret! //Encoding program //Pre-_____? //other necessary stuff here int main() { //create a string to encrypt using a char array cout<< "Original string is: "<<string<<endl; encrypt(string); cout<< "Encrypted string is: "<<string<<endl; decrypt(string); cout<<"Decrypted string is: "<<string<<endl; return 0; } void encrypt(char e[]) { //Write implementation...
Please write a complete C coding program (NOT C++) that has the following: (including comments) -...
Please write a complete C coding program (NOT C++) that has the following: (including comments) - declares two local integers x and y - defines a global structure containing two pointers (xptr, yptr) and an integer (z) - declares a variable (mst) by the type of previous structure - requests the values of x and y from the user using only one scanf statement - sets the first pointer in the struct to point to x - sets the second...
Complete the provided partial C++ Linked List program. Main.cpp is given and Link list header file...
Complete the provided partial C++ Linked List program. Main.cpp is given and Link list header file is also given. The given testfile listmain.cpp is given for demonstration of unsorted list functionality. The functions header file is also given. Complete the functions of the header file linked_list.h below. ========================================================= // listmain.cpp #include "Linked_List.h" int main(int argc, char **argv) {      float           f;      Linked_List *theList;      cout << "Simple List Demonstration\n";      cout << "(List implemented as an Array - Do...
Modify the attached TicTacToeStart.cpp Your job is to complete three functions init, printBoard, and gameOver #include...
Modify the attached TicTacToeStart.cpp Your job is to complete three functions init, printBoard, and gameOver #include <iostream> using namespace std; char gameBoard[3][3]; void init(){     /*     using a double loop to initialize all gameBoard element as 'E'     */ } void printBoard() {     /*using a double loop to print out gameboard     */ } bool gameOver() {     /*if any row or column, or diagonal are all X or O, return true*/     return false; } int main()...
Write a complete JAVA program, including comments (worth 2 pts -- include a good comment at...
Write a complete JAVA program, including comments (worth 2 pts -- include a good comment at the top and at least one more good comment later in the program), to process data for the registrar as follows: NOTE: Include prompts. Send all output to the screen. 1. Read in the id number of a student, the number of credits the student has, and the student’s grade point average (this is a number like 3.25). Print the original data right after...
Take the following program and include overload functions into it. Display result of function. C++ Program:...
Take the following program and include overload functions into it. Display result of function. C++ Program: #include <iostream> using namespace std; // this is the additional function string read() {     string input;     cout << "Enter input: ";     cin >> input;     return input; } int main() {     // call function here     string output = read();     cout << "You entered: " << output; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT