Question

In: Computer Science

c++ programming What will the following C++ code display? int numbers[4]={99,87}; cout<<numbers[3]<<endl; 87                     &nbs

c++ programming

  1. What will the following C++ code display?

int numbers[4]={99,87};

cout<<numbers[3]<<endl;

  1. 87                            b. 0                                  c. garbage                        d. an error
  1. What is the output of the following program segment?

double mylist[5];

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

mylist[i]= (pow(i,2)+ 1 /2.0;

cout<<fixed<<showpoint<<setprecision(2);

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

{ cout<<setw(5)<<mylist[i];

}

              a. .5           1.5        4.5        9.5        16.5

              b. 0.50      1.50      4.50      9.50     16.50

             c. 0.50      1.50      8.50     27.50     64.50

            d. 0.50      1.50      2.50      3.50      4.50

     15. what is stored in list after the following C++ statement is executed?

     int list [8];

list[0]=1;

list[1]=2;

for(int i=2; i<7; i++)

{

     list[i]= list[i-1] * list[i-2];

     cout<<setw(5)<<list[i];

}

  1. 2    4    8   16   32          b. 2    4    8   16 256         c. 2    4    8   64 256        d. 2    4    8   32 256

  1. What is the output of the following C++ segment?

      int i;

int list [8];

list[0]=1;

list[1]=2;

for(int i=2; i<7; i++)

{

     list[i]= list[i-1] * list[i-2];

     cout<<setw(5)<<list[i];

}

for(int i=2; i<7; i++)

{

if( i>3)

    list[i]= list[i]- list[i-1];

cout<<setw(5)<<list[i];

}   

    a.    2    4    16   32 256    2    4    8   28   228      b. 2    4    4   16 256    2    4    4   28      228

   c. 2    4    8   32       256     2     2    4   28 228      d. 2    4    8   32 256    2    4    4   28     228

Given int numbers[]= { 12,145,567,100,1001};

  1. Write a function to indicate the position of the highest data?
  2. Write a function to determine the maximum value of the numbers [];

Given:

struct nameType { string first_name;   string last_Name;      string middle_Name;   };

struct addressType { string address; string city; string state; string zip; };

struct courseType { string course_Name; int course_number; string major; };

struct dateType { int month; int day; int year;   };

struct studentType { nameType name; int student_ID; addressType address; dateType enrolled;};

  1. How many of the following are invalid ?

studentType student;

studentType array[89];

courseType course;

nameType name;

student.gpa=4.0;

student.name.first_name="Robert";

student.course.course_Name="Chem";

array[0].enrolled=student.enrolled;

array[1].name.first_name="Jane";

  1. 1                                        b. 3                             c. 2.                                  d. none
  1. How of the following statements are invalid ?

studentType student;

studentType array[89];

courseType course;

nameType name;

student.gpa=4.0;

student.name="Robert";

student.course.="Chem";

array[0].enrolled=student.enrolled;

array[1].name.first_name="Jane";

  1. 1                                     b. 3                                  c. 2                                   d. none

Solutions

Expert Solution

Question 1:

   int numbers[4] = {99,87};
   cout<<numbers[3]<<endl;

   As numbers[3] is not set so it will 0.
   ans: option B
   0

Question 2:

   double mylist[5];
   for(int i=0; i<5; i++)
   {
       mylist[i]= (pow(i,2)+ 1 /2.0);
       when any of the divisor or dividend is float then division is complete division.
       1st loop i=0 mylist[0] = 0 + 1/2.0 = 0.50
       2nd loop i=1 mylist[1] = 1 + 1/2.0 = 1.50
       3rd loop i=2 mylist[2] = 2 power 2 + 1/2.0 = 4 + 1.50 = 4.50
       4th loop i=3 mylist[3] = 3 power 2 + 1/2.0 = 9.50
       5th loop i=4 mylist[4] = 4 power 2 + 1/2.0 = 16.50  
   }

   ans: option b
   0.50 1.50 4.50 9.50 16.50
  
Question 3:

   ans: option d
   2 4 8 32 256
  
Question 4:

   ans: option d
   d. 2 4 8 32 256 2 4 4 28 228
  
Question 5:
  

#include<bits/stdc++.h>
using namespace std;
int highest(int arr[], int n) 
{ 
    int i; 
    int max = arr[0];  
    for (i = 1; i < n; i++) 
        if (arr[i] > max) 
            max = arr[i]; 
    return max; 
} 
int main()
{
    int numbers[]= { 12,145,567,100,1001};
    int n = sizeof(numbers) / sizeof(numbers[0]); 
    cout << "Largest element given array is "<< highest(numbers, n);
    return 0; 
}

Output:


Related Solutions

Translate c++ code into mips assembly: int main() {                 cout << "Numbers:" << endl;            &nbs
Translate c++ code into mips assembly: int main() {                 cout << "Numbers:" << endl;                                 int x[] = {18, 12, 6, 500, 54, 3, 2, 122};                 int i;                                 for (i=0; i<8; i++)                 {                                                 cout << x[i] << endl;                 }                 return 0; } below is my code: .data        str1: .ascii "Numbers:"     str2: .ascii "\n"    x: .word 18,12,6,500,54,3,2,122       .text                      ...
***Convert the C++ to Python*** #include <iostream> using std::cout; using std::cin; using std::endl; int charClass; char...
***Convert the C++ to Python*** #include <iostream> using std::cout; using std::cin; using std::endl; int charClass; char lexeme[100]; char str[200]; char nextChar; const int LETTER = 0; const int DIGIT   = 1; const int UNKNOWN = -1; const int OPAREN = 2; const int CPAREN = 3; const int PLUS = 4; const int MINUS = 5; const int MUL = 6; const int DIV = 7; const int ID_CODE = 100; const int PLUS_CODE = 101; const int MINUS_CODE =...
1. What will be the value of numbers[1] after the following code is executed? int[] numbers...
1. What will be the value of numbers[1] after the following code is executed? int[] numbers = {22, 33, 44}; for(int k = 0; k < 3; k++) { numbers[k] = numbers[k] + 5; } } 2. What will be the results of the following code? final int ARRAY_SIZE = 5; double[] x = new double[ARRAY_SIZE]; for(int i = 1; i <= ARRAY_SIZE; i++) { x[i] = 10.0; } 3.What is the value of numbers [3] after the following line...
Answer the following What is the difference between these two methods? Void display(int x) Int display(int...
Answer the following What is the difference between these two methods? Void display(int x) Int display(int s) Write a one line of code to generate random numbers from 35 to 55.( not a full program) Draw the UML diagram for a switch statement with 4 cases . Write a while loop to output the value of the variable x starting from 5, decreasing it by 0.5 each time, as long as x remains positive. Write a for loop to display...
C Programming Please modify this code to display the "guess" value from the calculation and the...
C Programming Please modify this code to display the "guess" value from the calculation and the "iterations" it took to complete. Please also add the input to the program. Input: Program will prompt the user to provide the desired decimal-place accuracy, in the range of [1-15], inclusive. User prompt must clearly indicate what is a valid input. User input must be validated - ensuring that the value entered is an integer in the above range. Sample output for one perfect...
What is the output of the following C++ code? int* length; int* width; length = new...
What is the output of the following C++ code? int* length; int* width; length = new int; *length = 5; width = length; length = new int; *length = 2 * (*width); cout << *length << " " << *width << " " << (*length) * (*width) << endl;
3. Translate the following C code to MIPS assembly code (in two separate files). int main()...
3. Translate the following C code to MIPS assembly code (in two separate files). int main() { printf(“before subroutine!\n”); Subfunc(); printf(“after subroutine!\n!”); } void Subfunc() {printf(“I am subroutine!\n”);} Submission file: Lab4_3a.asm for the main routine and Lab4_3b.asm for the sub-routine.
3. Translate the following C code to MIPS assembly code (in two separate files). int main()...
3. Translate the following C code to MIPS assembly code (in two separate files). int main() { printf(“before subroutine!\n”); Subfunc(); printf(“after subroutine!\n!”); } void Subfunc() {printf(“I am subroutine!\n”);} 4. Translate the following C code to MIPS assembly (in two separate files). Run the program step by step and observe the order of instructions being executed and the value of $sp. int main() { int x=2; z=Subfunc(x); printf(“Value of z is: %d”, z); } int Subfunc(int x) { return x+1;}
Q1: Given the following code, what is returned by tq(4)? int tq(int num){ if (num ==...
Q1: Given the following code, what is returned by tq(4)? int tq(int num){ if (num == 0) return 0; else if (num > 100) return -1; else     return num + tq( num – 1 ); } Group of answer choices: 0 4 -1 10 Q2: Given that values is of type LLNode<Integer> and references a linked list (non-empty) of Integer objects, what does the following code do if invoked as mystery(values)? int mystery(LLNode<Integer> list) {    if (list.getLink() ==...
Translate the following function f to MIPS assembly code. int f(int a, int b, int c,...
Translate the following function f to MIPS assembly code. int f(int a, int b, int c, int d) { return func(func(a,b), func(b+c,d)); } Assume the followings. • The prototype of function func is “int func(int a, int b);”. • You do not need to implement function func. The first instruction in function func is labeled “FUNC”. • In the implementation of function f, if you need to use registers $t0 through $t7, use the lower-numbered registers first. • In the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT