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

Object Oriented Programming (W4)

Lab 1
Tuesday; March 28, 2023
Instructor: Arifah Azhar
Email: arifah.azhar@umt.edu.pk

OBJECTIVES THINGS TO REMEMBER


 Learn to debug/dry run programs  Indent your code
 Switch – case  Comment your code
 If based selection  Use meaningful variable names
 for loops  Plan your code carefully before coding
 while loops  Do not take input or output inside a
 do while loops function unless it is an input or output
 NULL terminated strings function

In-lab module [CLO 1] [20 minutes]

QUESTION 1

Create a switch based where the user will be prompted again and again to choose the appropriate
question to execute. If he does not wish to make any more executions, he should enter -1.
The menu should properly prompt you about the question number you want to execute. After
successful execution of the question, the menu should be displayed again. In case the question
number is invalid, your program should be able to say so.

QUESTION 2 [20 minutes]


Write a C++ program that prompts the user to input a four digit positive integer. The program then outputs the
digits of the number, one digit per line.

For example, if the input is 324, the output is


3
2
4

page 1 of 6
Self-study time: we ae there to help you, if you ask for it.

PRE-DEFINED STRING LIBRARY


 <string> //exclusive to c++
 <cstring> / “string.h” //c specific

You can read more about these libraries from MSDN, your book or the internet.

Some of the most commonly used functions from cstring are:

page 2 of 6
page 3 of 6
2a. What is the output of the following program : [CLO2] [10 minutes]

#include "string.h"
#include "iostream.h"
void main()
{
char S1[100];
char S2[100];
char S3[100];
int a;
strcpy(S3,"AAA");
strcpy(S1,"HELLO ");
strcpy(S2,"THERE ");
cout << "\nFIRST TIME " << "\t S1 = " << S1 << "\t S2 = " << S2 << "\t S3 = " << S3;
strncpy(S2,S3,2);
cout << "\nSECOND TIME " << "\t S1 = " << S1 << "\t S2 = " << S2 << "\t S3 = " << S3;
strncpy(S2,S3,5);
cout << "\nSECOND TIME " << "\t S1 = " << S1 << "\t S2 = " << S2 << "\t S3 = " << S3;
strcat(S3,S1);
cout << "\nTHIRD TIME " << "\t S1 = " << S1 << "\t S2 = " << S2 << "\t S3 = " << S3;
strncat(S3,S1,2);
cout << "\nFOURTH TIME " << "\t S1 = " << S1 << "\t S2 = " << S2 << "\t S3 = " << S3;
strncat(S3,S1,10);
cout << "\nFIFTH TIME " << "\t S1 = " << S1 << "\t S2 = " << S2 << "\t S3 = " << S3;
a = strcmp(S2,S1);
cout << "\nSIXTH TIME " << "\t a = " << a;

a = strcmp(S2,S3);
cout << "\nSEVENTH TIME " << "\t a = " << a;

a = strncmp(S2,S3,3);
cout << "\nEIGHTH TIME " << "\t a = " << a;

a = strncmp(S2,S3,4);
cout << "\nNINTH TIME " << "\t a = " << a;
a = strncmp(S2,S3,8);
cout << "\nTENTH TIME " << "\t a = " << a;
}

page 4 of 6
2b. What is the output of the following piece of code? [CLO 2] [10 minutes]

const int SIZE = 30;

void main()
{
char str1[SIZE]="\0";
char str2[SIZE]="\0";
char str3[SIZE] = "\0";
int i;

for (i=0;i<'9'-'0'+1;++i)
str1[i] = '9' - i;

cout << str1 << '\n';

for (i=0;i<'9'-'0'+1;++i)
str2[i] = '0' + i;

cout << str2 << '\n';

for (i=0;i<strlen(str1)+strlen(str2);i=i+2)
{ str3[i] = str1[i];
str3[i+1] = str2[i];
}

cout << str3 << '\n';


}

page 5 of 6
2C. What is stored in S1 for getline for the following inputs [CLO 3] [10 minutes]

char S1[100];

cin.getline(S1,5,'\n'); //user inputs abcdefghi

cin.getline(S1,5,'\n'); //user inputs abc

cin.getline(S1,5,'a'); //user inputs bbbccddda

cin.getline(S1,5,'a'); //user inputs bba 

2d. what is the outpu of the following code [clo 2] [5 minutes]

page 6 of 6

You might also like