Question

I have to type and explain in class each code in every detail filled with //...

I have to type and explain in class each code in every detail filled with // commentary.

Explains how does work in every codes.

1)

What does the below print

#include <iostream>
using namespace std ;

int main()
{

   int var1 = 20 ;
   int var2 = 30 ;

   int* ptr1 ;
   int* ptr2 ;
   int* temp ;


   ptr1 = &var1 ;
   ptr2 = &var2 ;

   cout << *ptr1 << endl ;
   cout << var2 << endl ;
   cout << *ptr2 << endl ;

   temp = ptr1 ; ptr1 = ptr2 ;
   ptr2 = temp ;

   cout << *ptr1 << endl ;
   cout << var1 << endl ;
   cout << *ptr2 << endl ;
}

2)

What does the below print ?

#include <iostream>

using namespace std ;




int main()
{

int arr1[2][3] = { {1,2,3} , { 4,5,6}    } ;

for( int i1=0 ; i1<3 ; i1++ )
   {
     arr1[0][i1] = arr1[0][i1] + 1 ;
     arr1[1][i1] = arr1[1][i1] - 1 ;

   }

for( int i1=0 ; i1<2 ; i1++ )
   {
     for( int i2=0 ; i2<3 ; i2 = i2 +2 )
       {
          cout << arr1[i1][i2] << " " ;
       } //for
     cout << endl ;

   } //for



}

3)

What does the following print ?

#include <iostream>

using namespace std ;


void showLocal()
{
static int localNum = 5 ;

cout << "localNum is " << localNum << endl;
localNum++ ;
}


void showLocalOne()
{
int localNum = 5 ;

cout << "localNum in showLocalOne is " << localNum << endl;
localNum++ ;
}



int main()
{
   showLocal();
   showLocal();

   showLocalOne();
   showLocalOne();



   return 0;
}

4)

The following program uses linear search to search a particular section of an array. What will it print ?


#include <iostream>

using namespace std ;


int searchList( const int list[], int size, int value, int leftIndex, int rightIndex )
{
   int index = 0;        // Used as a subscript to search array
   int position = -1;    // To record position of search value
   bool found = false;   // Flag to indicate if the value was found


   for( int i1=leftIndex ; i1<= rightIndex ; i1++ )
   {
    cout << list[i1] << " : " ;
      if (list[i1] == value)   // If the value is found
      {
        cout << list[i1] << " : " ;
        return ( i1 ) ;
      }
                         // Go to the next element
   }
   return position;               // Return the position, or -1
}



int main()
{
    int arr1[] = { 5 , 2 , 1 , 9 , 3 , 4 } ;

    searchList (   arr1 , 6, 3 , 2, 4 ) ;
    cout << endl ;

    searchList (   arr1 , 6, 3 , 1, 3 ) ;
    cout << endl ;
    searchList (   arr1 , 6, 10 , 1, 3 ) ;
    cout << endl ;

    return ( 0 ) ;
}

5)

What does the following print ?

#include <iostream>

using namespace std ;


int binarySearch(const int array[], int numElems, int value)
{
    int first = 0,   // First array element
        last = numElems - 1,   // Last array element
        middle,   // Midpoint of search
        position = -1;   // Position of search value
    bool found = false;   // Flag

    while (!found && first <= last)
    {
        middle = (first + last) / 2;   // Calculate midpoint
        cout << array[middle] << " : " ;
        if (array[middle] == value)   // If value is found at mid
        {
           found = true;
           position = middle;
        }
        else if (array[middle] > value) // If value is in lower half
           last = middle - 1;
        else
           first = middle + 1;   // If value is in upper half
    }

    cout << position << " : " ;
    return position;
}




int main()
{
    int arr1[] = { 1 , 2 , 3 , 4 , 6 , 9 } ;

    binarySearch(    arr1 , 6, 3   ) ;
    cout << endl ;

    binarySearch(    arr1 , 6, 5 ) ;
    cout << endl ;
    binarySearch(    arr1 , 6, 1 ) ;
    cout << endl ;

    return ( 0 ) ;
}

6)

What does the following program print ?

#include <iostream>

using namespace std ;

void printArray( int array1[], int size )
{
for( int i1=0 ; i1<size ; i1++ )
    {
       cout << array1[i1] << " " ;

    }
cout << endl ;

}

void selectionSort( int array[], int size )
{
    int startScan, minIndex, minValue;
    for (startScan = 0; startScan < (size - 1); startScan++)
    {
        minIndex = startScan;
        minValue = array[startScan];
        for(int index = startScan + 1; index < size; index++)
        {
            if (array[index] < minValue)
            {
                minValue = array[index];
                minIndex = index;
            }
        }

        array[minIndex] = array[startScan];
        array[startScan] = minValue;
        printArray( array , size ) ;
    }
}






int main()
{
    int arr1[] = { 5 , 8 , 2 , 7 } ;

    selectionSort(    arr1 , 4 ) ;
    cout << endl ;

    return ( 0 ) ;
}

7)

#include <iostream>
#include <cstring>

using namespace std ;







int main()
{
    char ch1 = 'a' ;
    char buffer[256] ;

    cout << char( toupper( ch1 ) ) << endl ;
    cout << char( toupper( ch1 + 1 ) ) << endl ;
    cout << char( tolower( ch1 + 2 ) ) << endl ;

    strcpy ( buffer, "Henry " ) ;
    strcat ( buffer , "Armstrong" ) ;

    cout << strlen( buffer ) << " : " <<
        buffer << endl ;


    return ( 0 ) ;
}

8)

What does the following print ?

#include <iostream>
#include <cstring>

using namespace std ;



int main()
{
    int arr1[] = { 10 , 20, 30, 40 } ;
    int* ptr1 ;
    int index = 0 ;

    ptr1 = arr1 ;
    index++ ;

    cout << *( ptr1 + ++index ) << endl ;
    cout << *( arr1 + ++index ) << endl ;
    cout << arr1[--index] << endl ;
    cout << ptr1[--index] << endl ;


    return ( 0 ) ;
}

9)

What does the following print ?

#include <string>
#include <iostream>

using namespace std;

struct EmployeePay
{
string name;       // Employee name
int empNum;       // Employee number
double payRate;    // Hourly pay rate
double hours;      // Hours worked
double grossPay;    // Gross pay
};

void print( EmployeePay emp )
{
cout << emp.name << " : " << emp.empNum << " : " <<
    emp.payRate << " : " << emp.hours << " : " << emp.grossPay <<
    endl ;

}

int main()
{
EmployeePay employee1 = {"Betty Ross", 1, 18.75   }   ;
EmployeePay employee2 = {"Jill Sandburg", 2, 40, 10 , 400   } ;

print( employee1 ) ;
print( employee2 ) ;
}

10)

What will the following print ?

#include <iostream>

using namespace std ;


class Demo
{
public:
int x1 ; int x2 ;

Demo(int x1p, int x2p)
{
   x1 = x1p ;
   x2 = x2p ;
   cout << "Inside the constructor." << endl ;
   cout << "x1:" << x1 << " x2:" << x2 << endl ;
}

   ~Demo()
   {
       cout << "Inside the destructor." << endl ;
       cout << "x1:" << x1 << " x2:" << x2 << endl ;

   }


};


int main()
{
cout << "Before creating the object." << endl ;
Demo demo1Obj ( 2,5 ) ;
cout << "After creating the object." << endl ;

Demo demo2Obj ( 3, 6 ) ;





}

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

/******************************************************************************

Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
using namespace std ;

int main()
{
//variable declarations
int var1 = 20 ;
int var2 = 30 ;
//pointer varaible declaration
int* ptr1 ;
int* ptr2 ;
int* temp ;


ptr1 = &var1 ;//pointing ptr1 to the address of var1
ptr2 = &var2 ;//pointing ptr 2 to the address of var2

cout << *ptr1 << endl ;//prints 20, because ptr1 points to address of var1, and *ptr1 means value at address of var1
cout << var2 << endl ;//prints 30
cout << *ptr2 << endl ;//prints 30

temp = ptr1 ;//now temp points to address of var1
ptr1 = ptr2 ;//ptr1 points to var2
ptr2 = temp ;//ptr2 points to var1

cout << *ptr1 << endl ;//prints 30
cout << var1 << endl ;//prints 20
cout << *ptr2 << endl ;//prints 20
}

Output:

20

30

30

30

20

20

Note: As per Chegg policy, I am allowed to answer only 1 question(including sub-parts)

on a single post, kingly post the remaining questions seperately and i will try to answer them

Sorry for the inconvenience caused, thank you

Add a comment
Know the answer?
Add Answer to:
I have to type and explain in class each code in every detail filled with //...
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
  • I need help of how to include the merge sort code into this counter code found...

    I need help of how to include the merge sort code into this counter code found below: #include<iostream> using namespace std; int bubble_counter=0,selection_counter=0; // variables global void bubble_sort(int [], int); void show_array(int [],int); void selectionsort(int [], int ); int main() { int a[7]={4,1,7,2,9,0,3}; show_array(a,7); //bubble_sort(a,7); selectionsort(a,7); show_array(a,7); cout<<"Bubble counter = "<<bubble_counter<<endl; cout<<"Selection Counter = "<<selection_counter<<endl; return 0; } void show_array(int array[],int size) { for( int i=0 ; i<size; i++) { cout<<array[i]<< " "; } cout<<endl; } void bubble_sort( int array[],...

  • How can i make a counter for the number of exchanges made in the linear algorithm?? The binary counter works but the lin...

    How can i make a counter for the number of exchanges made in the linear algorithm?? The binary counter works but the linear doesn't. Here's my code. #include <iostream> using namespace std; void selectionSort(int[], int, int& ); void showSelection(int[], int); void sortArray(int[], int, int&); void showArray(const int[], int); int main() {    int values[25] = { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24...

  • read in numbers into array , print out, sort - my program reads in and prints...

    read in numbers into array , print out, sort - my program reads in and prints out but crashes before the sort - crashes after it prints out the scores - *important I have to use pointers!!!! c++ #include <iostream> using namespace std; void sortarray(int *tarray, int ); void displayarray(int *[], int); void averageshowarray(int *[], int); int main() {     int Size;     int count;     cout << "enter the size: " << endl;     cin >> Size;     int...

  • C++ 1. A?B?C?D? which one is correct 2. 3A, 3B #include<iostream> using namespace std; void swap0(int*...

    C++ 1. A?B?C?D? which one is correct 2. 3A, 3B #include<iostream> using namespace std; void swap0(int* ptri, int* ptr2) { int *temp; temp = ptr1; ptr1 = ptr2; ptr2 = temp; void swap1(int ptri, int ptr2){ int temp; temp = ptri; ptr1 = ptr2; ptr2 = temp; portion void swap2(int *&ptri, int *&ptr2){ int* temp; temp = ptr1; ptr1 = ptr2; ptr2 = temp; void swap3(int &ptri, int &ptr2) { int temp; temp = ptr1; ptr1 = ptr2; ptr2 =...

  • One dimensional array What this code print #include <iostream> using namespace std; int main () {...

    One dimensional array What this code print #include <iostream> using namespace std; int main () { const int SIZE = 7; int numbers [SIZE] = {1, 2, 4, 8): // Initialize first 4 elements cout << “Here are the contents of the array:\n"; for (int index = 0; index < SIZE: index++} cout << numbers[index] << “ “; cout << endl; return 0; }

  • 15.6: Fix the code to print the count from 1 to x (to loop x times)...

    15.6: Fix the code to print the count from 1 to x (to loop x times) #include <iostream> // include the header file using namespace std; void count(int x){    for(int i=1; i<=x; i++){ cout<<x; } } int main(){    int x,i;    cin >> x; count(x);    return 0; } 15.7 Fix the nested for loops to print the count from 1 to x twice, e.g. "12....x12.....x" #include <iostream> // include the header file using namespace std; int main(){...

  • Explain the results 푤include using nanespace std: int nain (O rloat arr[s- (12.5,.11.0, 13.8, 1...

    Explain the results 푤include <iostream> using nanespace std: int nain (O rloat arr[s- (12.5,.11.0, 13.8, 1000.5, 1.5): float ptri-arEIo1: float *ptr2"ptri +3: cout<< +ptr2xcend *ptr2-* (ptr2)+1: //Line 16 cout<< ptr2<<endl: return 0; Bxplaing the results. 5 What Happens if the Line 16 is replaced by *ptr2- (ptr2+1): 6. tincludekiostream> using namespace std: void fun (int arr(I) int for air size = arestizer) isizeor (arr10]); for (i=0;遠くarr size: i++) int main(0 int int array 114](10, 20 30, 401 fun (array 1):...

  • what is the output for the following code? explain the steps. /*#include <iostream> using namespace std;...

    what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...

  • Consider the following C++code snippet and what is the output of this program? # include<iostream> using...

    Consider the following C++code snippet and what is the output of this program? # include<iostream> using namespace std; void arraySome (int[), int, int); int main () const int arraysize = 10; int a[arraysize]-1,2,3,4,5, 6,7,8,9,10 cout << "The values in the array are:" << endl; arraySome (a, 0, arraySize) cout<< endl; system ("pause") return 0; void arraySome (int b[], int current, int size) if (current< size) arraySome (b, current+1, size); cout << b[current] <<""; a) Print the array values b) Double...

  • Write a C++ program that contains 2 functions. LastLargestIndex, that takes as paraneters an int array...

    Write a C++ program that contains 2 functions. LastLargestIndex, that takes as paraneters an int array and // its size and returns the index of the first occurrence of the largest element II in the array. Also, write a function to display the array #include ·peh.h" #include <iostream> using namespace std const int ARRAY_SIZE = 15; int main int list[ARRAY SIZE56, 34, 67, 54, 23, 87, 66, 92. 15, 32, 5, 54, 88, 92, 30 cout < List elements: "...

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
ADVERTISEMENT