CSC241 - Introducti On Program Ming II: Jahan Institute of Higher Education Department of Computer Sciences

You might also like

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

123-

JAHAN
INSTITUTE
OF HIGHER
EDUCATION
Department
of Computer
Sciences

CSC241
Introducti
on
Program
ming II

Name:
Registration ID:

Q1: Describe the difference between the literal values


8, "8", and 8.
We use 8 to assign as an integer variable in c++. 5 in
the statement is a literal constant and we cant assign
a value to 5 and its value cant be changed.
We assign 8 to prints 8 to the standard output
which is usually a terminal or screen display.
We assign 8 to be as a character in c++.
Q2: For each of the following statements, explain why
it is not correct, and fix it.

(a) #include <iostream.h>;


#include <iostream.h>
We must not use semicolon after
greatest sign
If we use this. then this is a syntax
erro

Assignmen
t2

(b)

int function(void arg1)


{
return arg1-1;
}

If we program the code like this the


compiler gives syntax two syntax
error
1- Value of type void is not allowed
2- Not an allowed type
Int function (void) {
Int arg1;
arg1=20;
Cout<<arg1;
Return arg1-1;
}

(c)

(d)

MAIN()
{
cout(Hello world;
}

int x = 5;
if ( x = 6 )
cout<<x is equal to,x;

If we program like this the compiler


will give a syntax error
1- Statement missing ;
We must write
Main()
{
Cout<<hello world;
}
If we write the program like this the
compiler will not show the output and
the equal sign in if statement is not
correct we must use double equal
sign. Because we must concatenating
X after double quotation mark and
also we write (else) after (if) to for
output if first statement was not
correct the else statement will work
and show no equal to x.
We must write
int x=5;
if(x==6)
cout<<"x is equal to"<<x;
else
cout<<"x is not equal to"<<x;

Q3: Write a simple c program to read and write a single character.


#include <iostream.h>
#include <conio.h>
void main(){
char c;
cout<<"enter character =";
cin>>c;
cout<<c;
getch();
}
Q4: Arrange the following c program in order and write the output.
Before Arranging
After Arranging
constint LENGTH = 10;
#include <conio.h>
#include<conio.h>
#include <stdio.h>
printf("value of area : %d", area);
int main()
#include <stdio.h>
{
const int WIDTH = 5;
int area;
getche();
const int LENGTH = 10;
const char NEWLINE = '\n';
const int WIDTH = 5;
int main()
const char NEWLINE = '\n';
area = LENGTH * WIDTH;
area = LENGTH * WIDTH;

int area;
{
return 0;
printf("%c", NEWLINE);
}

printf("value of area : %d", area);


printf("%c", NEWLINE);
return 0;
getche();
}

Q5: Write a program to find out the binary number of a decimal number.
#include <iostream.h>
#include <conio.h>
int main()
{
long intd,q;
int b[100],i=1,j;
cout<<"enter decimal number ";
cin>>d;
q = d;
while (q!=0) {
b[i++]=q%2;
q=q/2;
}
for(j=i-1;j>0;j--)
cout<<b[j];
getch();
}

Q6: Write a program that convert KM to M, CM and MM.


#include <iostream.h>
#include <conio.h>
void main() {
inth,km,m,cm,mm;
km=1000;
cout<<"enter number km =";
cin>>km;
cout<<endl;
m=km*1000;
cout<<"\t"<<km <<" km" <<" is equal to ="<<(m)<<" Meters."<<"\n";
cm=km*100000;
cout<<"\t"<<km <<" km" <<" is equal to ="<<cm<<"
Centimeters."<<"\n";
mm=km*1000000;
cout<<"\t"<<km <<" km" <<" is equal to
="<<mm<<"(Millimeters)"<<"\n";
getch();
}

Q7: Write a program to determine whether the number is a vowel or


consonant.
#include <iostream.h>
#include <conio.h>
void main()
{
char ch;
cin>>ch;
if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'||ch=='o'||ch=='O'||
ch=='u'||ch=='U'){
cout<<"vowel";
}
else if(ch=='w'||ch=='W'||ch=='y'||ch=='Y'){
cout<<"semi vowel";
}
else
cout<<"consonent";
getch();
}
Q8: Write a program that determines whether the number is an alphabet or
number.
#include <iostream.h>
#include <conio.h>
void main()
{
char ch;
cout<<"enter your choice";
cin>>ch;
if(ch>='0'&&ch<='9')
{
cout<<"number";
}
else if(ch>='a' &&ch<= 'z') {
cout<<"Alphabet";
}
else if(ch>='A' &&ch<='Z')
{
cout<<"Alphabet";
}
getch();
}
Q9: Write a program that prints that prints the table of 2 using FOR, WHILE
and DO-WHILE.

Q10: Write to a program to do the following.


Takes the marks of a students in 5 subjects.
Shows the total marks obtained by the student.
Find the average marks of the student.
#include <iostream.h>
#include <conio.h>
int main()
{
intma,is,eng,c,os,total,avg;
cout<<"enter math score =";
cin>>ma;
cout<<"enter islamic score =";
cin>>is;
cout<<"enter english score =";
cin>>eng;
cout<<"enter c++ score =";
cin>>c;
cout<<"enter operating system score =";
cin>>os;
total=ma+is+eng+c+os;
cout<<"The to total marks of the student ="<<total<<"\n";
avg=total/5;
cout<<"The average of the student is ="<<avg;
getch();
}
Q11: Find the errors:
Before fix
#includ<iostream>
int main()
{
int num1; num2,num3;
cout(" Enter value for first number;)"\
cin<<num1;
cout(" Enter value for second
number");
cin<<num2;
cout<" Enter value for third
number";
cin(num3);
IF(num1>num2&&num1>num3)
{
Cout<<" First number is
greatest:"<<endl<<"whick is=
"<<num1;
}

After fix
#include <iostream.h>
int main()
{
int num1, num2,num3;
cout<<" Enter value for first
number";
cin>>num1;
cout<<" Enter value for second
number";
cin>>num2;
cout<<" Enter value for third
number";
cin>>num3;
if(num1>num2&&num1>num3)
{
cout<<" First number is
greatest:"<<endl<<"whick is=
"<<num1;

elseif(num2>num1&&num2>num3)
{
Cout<" Second number is
greatest"<<endl<<"whick is=
"<<num2;
}}
else
{
cout<<" Third number is
greatest"<<endl<<"whick is=
"<<num3;
}
return 0;
}

}
else if(num2>num1&&num2>num3)
{
cout<<"Second number is
greatest"<<endl<<"whick is=
"<<num2;
}
else
{
cout<<" Third number is
greatest"<<endl<<"whick is=
"<<num3;
}
return 0;
}

Q11: Write a program that explains the use of storage classes.


A storage class defines the scope (visibility) and life-time of variables and/or
functions within a C++ Program. These specifiers precede the type that they modify.
There are following storage classes, which can be used in a C++ Program

auto

register

static

extern

The auto Storage Class


The auto storage class is the default storage class for all local variables.
{
int year;
autoint year;

}
The example above defines two variables with the same storage class, auto can only
be used within functions, i.e., local variables.

The register Storage Class


The register storage class is used to define local variables that should be stored in a
register instead of RAM. This means that the variable has a maximum size equal to
the register size (usually one word) and can't have the unary '&' operator applied to
it (as it does not have a memory location).
{
registerint kilometer;
}
The register should only be used for variables that require quick access such as
counters. It should also be noted that defining 'register' does not mean that the
variable will be stored in a register. It means that it MIGHT be stored in a register
depending on hardware and implementation restrictions.

The static Storage Class


The static storage class instructs the compiler to keep a local variable in existence
during the life-time of the program instead of creating and destroying it each time it
comes into and goes out of scope. Therefore, making local variables static allows
them to maintain their values between function calls.
The static modifier may also be applied to global variables. When this is done, it
causes that variable's scope to be restricted to the file in which it is declared.
In C++, when static is used on a class data member, it causes only one copy of that
member to be shared by all objects of its class.

#include <iostream>
// Function declaration
voidfunc(void);

staticint count = 10; /* Global variable */


main()
{
while(count--)
{ func();
} return 0;
}// Function definition
voidfunc( void )
{ staticint i = 5; // local static variable
i++;
std::cout<< "i is "<< i ;
std::cout<<" and count is " << count <<std::endl;
}

The extern Storage Class


The extern storage class is used to give a reference of a global variable that is
visible to ALL the program files. When you use 'extern' the variable cannot be
initialized as all it does is point the variable name at a storage location that has been
previously defined.
When you have multiple files and you define a global variable or function, which will
be used in other files also, then extern will be used in another file to give reference of
defined variable or function. Just for understanding extern is used to declare a global
variable or function in another file.
The extern modifier is most commonly used when there are two or more files sharing
the same global variables or functions as explained below.

First File: main.cpp


#include <iostream>
int count ;
extern void write_extern();
main(){
count = 5;
write_extern();}
Second File: support.cpp
#include <iostream>
externint count;
voidwrite_extern(void)
{
std::cout<< "Count is "<< count <<std::endl;}

Q12: Write a program that explain the concept of structure in C++.

Thank You!!!

You might also like