Question

C++ ONLY! Consider the following code and answer the questions in the table below.

#include <iostream>

template <typename T, int N=10>

class Array {

private:

  T array[N+1];

public:

  Array() {

    for(int i=0; i<N+1; i++) array[i] = 0;

  }

  

  T& operator [] (int index) {

    if (index>N || index < 0)

      return array[N];

    else

      return array[index];

  }

  template <typename S>

  Array<T,N>& operator= (S &other) {

    for(int i=0; i<N+1; i++)

      array[i] = other[i];    

    return *this;

  }

};

template <typename T, typename S> T operator+ (T &t, S &s)

{

  T res;

  for(int i=0; i<10; i++)

    res[i] = t[i] + s[i];  

  return res;

}

template <typename T, int N> std::ostream& operator << (std::ostream& out, Array<T,N> &array)

{

  out << "{ ";

  for(int i=0; i<N; i++)

    {

      if (i>0) out << ", ";

      out << array[i];

    }

  out << "}\n";

  return out;

}

int main() {

   // code snippet runs here

   return 0;

}

Prompt Snippet Output / Error / Explanation What is the template specialization for variable x? Array int> x; 2 What is the ostd::cout << i[10]; Whats wrong with this code, and how would you fix it without changing the class Array template? Array<in

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

Question-1

This code snippet willnot throw any error nor its shows any ouput.

Given Array<int> x;

Here the template specialization for variable is int data type.
T in the template definition is substituted with int.


Question-2

Array<int,5> x;
std::cout<<x;

Output is

{ 0, 0, 0, 0, 0}

Question-3

Array<int> i;
Array<double> d;
i[1]=10.5; d[1] = 5.5;
auto res = d+i;
std::cout<<res;

Output is

{ 0, 15.5, 0, 0, 0, 0, 0, 0, 0, 0}

Out put is 15.5 because though we give i[1] =10.5 it will take it as 10, because it is int type. This will be added to 5.5 and gives 15.5


Question-4

Array<int> i;
Array<double> d;
i[1]=10.5; d[1] = 5.5;
auto res = d+i;
std::cout<<res;

res must be deduced to Array<double> type since the result is of double type.

Then the code snippet is

Array<int> i;
Array<double> d;
i[1]=10.5; d[1] = 5.5;
Array<double> res = d+i;
std::cout<<res;


Question-5

Array<int> cvt;
char hello[10] = "yoyoyoyo!";
cvt= hello;

This code snippet willnot throw any error nor its shows any ouput.

Here cvt is an int Array, but hello is assigned to it.
So compiler converts the ASCI value of each character and assigns it to particular blocks of the array.

Note:- Please ask remaining questions as another question.

Feel free to ask any doubts, if you face any difficulty in understanding.

Please upvote the answer if you find it helpful


Add a comment
Know the answer?
Add Answer to:
C++ ONLY! Consider the following code and answer the questions in the table below. #include <iostream>...
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
  • 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...

  • Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std;...

    Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std; int main(){    int rows = 5; int cols = 5; int x;    int** arr = new int[rows][cols]    cin >> x; arr[x][x] = x; cout << "arr[x][x] = " << arr[x][x];    return 0; } Question 2: Fix the code to initialize the 2D array elements to x #include <iostream> using namespace std; int main(){    int rows; int cols; int x;...

  • Redesign your Array class from lab6 as a class template to work with the application below....

    Redesign your Array class from lab6 as a class template to work with the application below. write your overloaded output stream operator as an inline friend method in the class declaration. Include the class template header file in your application as below. #include "Array.h" main() {   Array<char> c(3);   c.setValue(0,'c');   c.setValue(1,'s');   c.setValue(2,'c');   cout << c;   Array<int> i(3);   i.setValue(0,1);   i.setValue(1,2);   i.setValue(2,5);   cout << i;   Array<int> j(3);   j.setValue(0,10);   j.setValue(1,20);   j.setValue(2,50);   cout << j;   Array<int> ij;   ij = i + j;   cout << ij;...

  • #include <iostream> using namespace std; template <typename Item> class MyArray{ private:    Item *myarray;    int...

    #include <iostream> using namespace std; template <typename Item> class MyArray{ private:    Item *myarray;    int size;    int used;    void doubleSize(); public:    MyArray();    ~MyArray();    int length();    void insertHead(Item i);    void insertTail(Item i);    void deleteHead();    void deleteTail();    void sortAscending();    void sortDescending();    Item operator [](int i){        return myarray[i];    } }; template <typename Item> MyArray<Item>::MyArray(){    size = 5;    used = 0;    myarray = new Item[size];...

  • - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h"...

    - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h" template <typename DataType> StackArray<DataType>::StackArray(int maxNumber) { } template <typename DataType> StackArray<DataType>::StackArray(const StackArray& other) { } template <typename DataType> StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other) { } template <typename DataType> StackArray<DataType>::~StackArray() { } template <typename DataType> void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error) { } template <typename DataType> DataType StackArray<DataType>::pop() throw (logic_error) { } template <typename DataType> void StackArray<DataType>::clear() { } template <typename DataType> bool StackArray<DataType>::isEmpty() const {...

  • in c++ please program for this code #include <iostream> #include <fstream> #include <string> #include <cstring> //...

    in c++ please program for this code #include <iostream> #include <fstream> #include <string> #include <cstring> // for string tokenizer and c-style string processing #include <algorithm> // max function #include <stdlib.h> #include <time.h> using namespace std; // Extend the code here as needed class BTNode{ private: int nodeid; int data; int levelNum; BTNode* leftChildPtr; BTNode* rightChildPtr; public: BTNode(){} void setNodeId(int id){ nodeid = id; } int getNodeId(){ return nodeid; } void setData(int d){ data = d; } int getData(){ return data;...

  • In C++ please. Thank you!! #include <iostream> #include <cstring> // print an array backwards, where 'first'...

    In C++ please. Thank you!! #include <iostream> #include <cstring> // print an array backwards, where 'first' is the first index // of the array, and 'last' is the last index void writeArrayBackward(const char anArray[], int first, int last) { int i = 0; for (i = last; i >= first; i--) { std::cout << anArray[i]; } std::cout << std::endl; } // test driver int main() { const char *s = "abc123"; writeArrayBackward(s, 0, strlen(s) - 1); } // Using the...

  • //CODE 16-02.cpp //Demonstrates a template function that implements //a generic version of the selection sort algorithm....

    //CODE 16-02.cpp //Demonstrates a template function that implements //a generic version of the selection sort algorithm. #include <iostream> using std::cout; using std::endl; template<class T> void sort(T a[], int numberUsed); //Precondition: numberUsed <= declared size of the array a. //The array elements a[0] through a[numberUsed - 1] have values. //The assignment and < operator work for values of type T. //Postcondition: The values of a[0] through a[numberUsed - 1] have //been rearranged so that a[0] <= a[1] <=... <= a[numberUsed -...

  • 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; }

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