Classroom Ass1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Answer for the Assignment

1. What is the output of the following program after it


is executed?
const int size = 15;
char message[size];
cout << "Enter a sentence or phrase: “;
cin >> message; //suppose user entered the text: Please
get in.
cout << message << endl;

Enter a sentence or phrase: Please get in


Please

2. What is the output when the following code


fragment is executed in a complete c++ program?
char ch;
char title[] = "Titanic";
ch = title[1];
title[3] = ch;
cout << title << endl;
cout << ch << endl;

Titinic
i

3. What is the output of the following program after it


is executed?
#include <iostream>
#include <cstring>
using namespace std;
int main() {
struct student {
int age;
char name[25];
double gpa;
} stu;
student *ptr;
ptr=&stu;
stu.age = 123;
stu.gpa = 3.92;
strcpy(stu.name, "Natnael");
cout << stu.age << endl;
cout << stu.gpa << endl;
cout << stu.name << endl;
cout << (*ptr).age << endl;
cout << (*ptr).gpa << endl;
cout << (*ptr).name << endl;
return 0; }

123
3.92
Natnael
123
3.92
Natnael

4. What is the output of the following program after it


is executed?
#include <iostream>
using namespace std;
struct Time{
int hour;
int minute;
double second;
};
int main(){
Time *timePtr, time;
timePtr = &time;
cout<<"Enter time in hours: "; //assume 1
cin>>(*timePtr).hour;
(*timePtr).minute=time.hour*60;
(*timePtr).second=time.hour*60*60;
cout<<"\nDisplaying Time information\n";
cout<<"Time in hours: "<<time.hour<<endl;
cout<<"Time in minutes: "<<time.minute<<endl;
cout<<"Time in seconds: "<<time.second<<endl;
return 0;
}

Enter time in hours: 1

Displaying Time information


Time in hours: 1
Time in hours: 60
Time in hours: 3600

5. What is the output of the following program after it


is executed?
#include <iostream>
using namespace std;
struct Employee{
char name[20];
int experience;
double salary;
};
void displayData(Employee);
int main(){
Employee permanent;
cout<<"Enter full name: ";
cin.get(permanent.name, 20);
cout<<"Experience in years: ";
cin>>permanent.experience;
cout<<"Enter salary: ";
cin>>permanent.salary;
//Function call with structure as an arguments
displayData(permanent);
return 0;
}
void displayData(Employee p){
cout<<"\n Displaying Employee data \n ";
cout<<"Full name: "<<p.name<<endl;
cout<<"Experience: "<<p.experience<<"years"<<endl;
cout<<"Salary: "<<p.salary<<"Birr"<<endl;
}

Enter full name: Anaol Atinafu


Experience in years: 2
Enter salary: 200

Displying Employee data


Full name: Anaol Atinafu
Experience: 2years
Salary: 200Birr

6. What is the output of the following program after it


is executed?
#include <iostream>
using namespace std;
struct Employee{
char name[20];
int experience;
double salary;
};
void enterData(Employee &);//prototype
void displayData(Employee);//prototype
int main(){
Employee permanent;
//read in employee data
enterData(permanent); //read in
displayData(permanent); //display
return 0;
}
void enterData(employee &p){
cout<<"Enter full name: ";
cin.get(p.name, 20);
cout<<"Experience in years: ";
cin>>p.experience;
cout<<"Enter salary: ";
cin>>p.salary;
}
void displayData(Employee p){
couut<<"\n Displaying Employee data \n ";
cout<<"Full name: "<<p.name<<endl;
cout<<"Experience: "<<p.experience<<endl;
cout<<"Salary: "<<p.salary<<endl;
}

Enter full name: Anaol Atinafu


Experience in years: 2
Enter salary: 200

Displaying Employee data


Full name: Anaol Atinafu
Experience: 2
Salary: 200

7. What is the output of the following program after it


is executed?
#include <iostream>
using namespace std;
int main(){
int var1 =100;
int var2 = 200;
//display address of var1
cout<<"Address of var1: "<<&var1<<endl;
//display address of var2
cout<<"Address of var1: "<<&var2<<endl;}

Address of var1: 0x7fff730b6170


Address of var1: 0x7fff730b6174
8. What is the output of the following program after it
is executed?
#include <iostream>
using namespace std;
int main(){
double arr[2];
double *ptr;
cout<<"Displaying address using arrays: "<<endl;
for(int i=0; i<2<i++)
cout<<"&arr["<<i<<"] ="<<&arr[i]<<endl;
ptr=arr;
//use for loop to point addresses of all array elements
//using pointer notation
for(int i = 0; i<2; ++i){
cout<<"ptr + "<<i<<" = "<<ptr+i<<endl;
return 0;
}

Displaying address using arrays:


&arr[0] = 0x7fff67826c20
&arr[1] = 0x7fff67826c28
ptr + 0 = 0x7fff67826c20
ptr + 1 = 0x7fff67826c28

9. What is the output of the following program after it


is executed?
#include <iostream>
using namespace std;
void swapping(int &s1,int &s2){
int var=s1;
s1 = s2;
s2 = var;
}
int main(){
int a = 10, b =20;
cout<<"Before swaping\n";
cout<<"a ="<<a<<endl;
cout<<"b ="<<b<<endl;
swapping(a,b);
cout<<"After swaping\n";
cout<<"a ="<<a<<endl;
cout<<"b ="<<b<<endl;
return 0;
}
Before swaping
a =10
b =20
After swaping
a =20
b =10

10. What is the output? Show it in the right panel


#include <iostream>
using namespace std;
//function prototype with pinter as parameter
void swapping (int*, int*);
int main(){
int a = 10, b= 20;
cout<<"Before swapping the value"<<endl;
cout<<"a ="<<a<<endl;
cout<<"b ="<<b<<endl;
//call function by passing vaiable addresses
swapping(&a, &b);
cout<<"a ="<<a<<endl;
cout<<"b ="<<b<<endl;
return 0;
}
//function definition to swapping numbers
void swapping (int *s1, int *s2){
int var;
var = *s1;
*s1 = *s2;
*s2 = var;
}

Before swapping the values


a = 10
b = 20
a = 20
b = 10

11. What is the output of the following program after it


is executed?
//C++ Program to GPA of n number of students and
//display it where n is the number of students entered by
/the user
#include <iostream>
#include<cstring>
using namespace std;
int main(){
int num;
cout<<"Enter total number of students: ";
cin>>num;
double *ptr;
//memory allocation of num number of double
ptr = new double[num];
cout<<"Enter GPA of students: "<<endl;
for(int i = 0; i<num; ++i){
cout<<"GPA of students "<<i + 1<<": ";
cin>>*(ptr+i);
}
cout<<"\nDisplay the GPA of students"<<endl;
for(int i = 0; i<num; ++i){
cout<<"GPA of students "<<i + 1<<": "
<<*(ptr+i)<<endl;
};
//ptr memory is released
delete [ ] ptr;
}

Enter total number of students: 3


Enter GPA of students:
GPA of student 1: 3
GPA of student 2: 3.4
GPA of student 3: 3.6

Display the GPA of students


GPA of student 1: 3
GPA of student 2: 3.4
GPA of student 3: 3.6

12. What is the output of the following program after it


is executed?
#include <iostream>
using namespace std;
int main()
{
int var = 64, *ptr = &var;
char ch = ‘D’;
*ptr += ch;
cout << var << “, ” << ch << endl;
return 0;
}
132, D

13. What is the output of the following program after it


is executed?
#include <iostream>
using namespace std;
int main()
{
const int var = 20;
const int* const ptr = &var;
cout << var<<” “<< *ptr;
return 0;
}

20 20

14. What is the output of the following program after it


is executed?
#include <iostream>
using namespace std;
void fun(int *arr)
{
for(int i=0;i<5;i++){
*(arr)=i+*(arr);
}
}
int main(){
int arr=6;
fun(&arr);
cout<<arr<<” “;
return 0;
}

16

15. What is the output of the following program after it


is executed?
#include <iostream>
using namespace std;
int fun(int *x)
{
*x=5;
return *x;
}
int main()
{
int x = 10, y = 15, i;
for (i = 0; i < 1; i++) {
x = 20, y = 25;
}
int mul = x * y;
fun(&mul);
cout << *(&mul);
return 0;
}

15

16. What is the output of the following program after


it is executed?
#include <iostream>
void friendly();
void shy(int audienceCount);
int main() {
using namespace std;
friendly();
shy(6);
cout << "One more time:\n";
shy(2);
friendly();
cout << "End of program.\n";
return 0; }
void friendly() {
using namespace std;
cout << "Hello\n"; }
void shy(int audienceCount) {
using namespace std;
if (audienceCount < 5)
return;
cout << "Goodbye\n"; }

Hello
Goodbye
One more time:
Hello
End of program.
17.What is the output of the following program after it
#include <iostream>
void figureMeOut(int& x, int y, int& z);
int main() {
using namespace std;
int a=10, b=20, c=30;
figureMeOut(a, b, c);
cout << “a= “<<a << ", " <<”b= “<< b << ", " << c;
return 0; }
void figureMeOut(int& x, int y, int& z) {
using namespace std;
cout <<”x= “<< x << ", " <<”y= “<< y << ", " <<”z=
“<< z << endl;
x = 1;
y = 2;
z = 3;
cout <<”x= “<< x << ", " <<”y= “<< y << ", " <<”z=
“<< z << endl;
}

x = 10, y = 20, z = 30
x = 1, y = 2, z = 3
a = 1, b = 20, 3

18.What is the output produced after the following


program is executed?
#include <iostream>
using namespace std;
char mystery(int firstPar, int secondPar);
int main() {
cout << mystery(10, 9) << "ow\n";
return 0; }
char mystery(int firstPar, int secondPar) {
if (firstPar >= secondPar)
return 'W';
else
return 'H';
}

Wow

19. What is the output produced after the following


program is executed?
a) double x = fabs( -2.0 );
b) double x = fabs( 2.0 );
c) double x = log10( 7.389056);
d) double x = sqrt( 16.0 );
e) double x = log10(1000.0);
f) double x = sqrt( 36 .0);
g) int x = pow( 2, 3);

a) 2.0
b) 2.0
c) 0.86858895
d) 4.0
e) 3.0
f) 6.0
g) 8

20.What is the output produced after the following


program is executed?
typedef int* IntPtr; //Note that to use typedef
#include <cstddef>
int main( ) {
IntPtr ptr;
int arr[10];
int index;
for (index = 0; index < 10; index++)
arr[index] = index;
ptr = arr;
for (index = 0; index < 10; index++)
cout << ptr[index] << " ";
cout << endl;
for (index = 0; index < 10; index++)
p[index] = ptr[index] + 1;
for (index = 0; index < 10; index++)
cout << arr[index] << " ";
cout << “\nEnd”;
}

0123456789
1 2 3 4 5 6 7 8 9 10
End

21.What is the output produced after the following


program is executed?
int array_size = 10;
int *a;
a = new int [array_size];
int *ptr = a;
for (int i = 0; i < array_size; i++)
a[i] = i;
ptr[0] = 10;
for int (i = 0; i < array_size; i++)
cout << a[i] << ", ";
cout << “End”;

10, 1, 2, 3, 4, 5, 6, 7, 8, 9, End

22.What is the output produced after the following


program is executed?
int arraySize = 10;
int *a;
a = new int[arraySize];
for (int i = 0; i < arraySize; i++)
*(a + i) = i;
for (int i = 0; i < arraySize; i++)
cout << a[i] << ", ";
cout << “End”;

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, End

23.What is the output produced after the following


program is executed?
int arraySize = 10;
int *a;
a = new int[arraySize];
for (int i = 0; i < arraySize; i++)
a[i] = i;
while (*a < 9)
{
a++;
cout << *a << ", ";
}
cout << “End”;
}

0, 1, 2, 3, 4, 5, 6, 7, 8, End

1. Write a C++ program that accepts a long integer from the keyboard and displays
the number of digits to the screen. Implement the digit counter with a function taking
the number as an argument and returns the number of digits.
The output

2. Write a C++ program that accepts student’s name and father’s name; and prints
them in reverse order with a space between them
The code

The output

You might also like