Sei sulla pagina 1di 14

Assignment #02

PROGRAMMING IN LANGUAGE C/C++ (3577)

SUBMITTED TO: MR. Muhammad Asif

SUBMITTED BY:Abdul Shakoor

REG #:br540076

GROUP: A

PGD CS ONLINE PROGRAM (COMPUTER SCIENCE)

ALLAMA IQBAL OPEN UNIVERSITY ISLAMABAD


Question no 1(a):

Write a program to display a table of a number by the user as an input

Answer:

#include <iostream>

using namespace std;

int main()

int n;

cout <<"Enter a positive integer: ";

cin >> n;

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

cout << n <<" * "<< i <<" = "<< n * i << endl;

}
Question no 1(b):

What is the difference between while and do while loo? White a program to
demonstrate your answer

Answer:

While loop:

In C++ we use while loop when if we don’t know the condition in advance
that how many times loop will execute.

Syntax

while (condition)

Statements(…..);

Example:

#include <iostream>

using namespace std;

int main()

int number, i = 1, factorial = 1;

cout <<"Enter a positive integer: ";

cin >> number;

while ( i <= number)

factorial *= i; //factorial = factorial * i;

++i;

}
cout<<"Factorial of "<< number <<" = "<< factorial;

return 0;

Do while loop:

In C++ do while loop allows user to run program at-least one time without
any condition. In do while loop the program run run first then checks
condition

Syntax:

do

statements(…….);

while (condition);

Example:

#include <iostream>

#include <conio.h>

using namespace std;

int main()

int n,i,s;

i=1;

s=0;

cout <<"Enter n:";


cin >> n;

do

s=s+i;

i++;

while (i<=n);

cout <<"Sum of all from 1 to given number is = "<<s;

getch();

return 0;

Question no 2 :

Write a program that takes two integer from user as input and perform
following tasks

1. Addition

2. Multiplication

3. Subtraction

4. Division

5. Modulus

Define a different function for each operation

Answer:

#include<iostream>

using namespace std;

main()
{

int a,b,sum,sub,mult,div,mod;

cout<<"please enter first number : ";

cin>>a;

cout<<"please enter the second number : ";

cin>>b;

sum=a+b;

sub=a-b;

mult=a*b;

div=a/b;

mod=a%b;

cout<<"sum of given numbers are "<<sum <<endl;

cout<<"sub of given number is "<<sub<<endl;

cout<<"mult of given number is "<<mult<<endl;

cout<<"div of given number is "<<div<<endl;

cout<<"mod of given number is "<<mod<<endl;

In this program we take two inputs from user

Sum function add these numbers

Sub function subtract these numbers

Mult function multiply these numbers

Div function divide these numbers

Mod function shows remainder after division of these numbers


Question no 3:

Part no 1:

What are arrays? Write an example to demonstrate the concept of array

Answer:

C++ provide a data structure the array which allows user to store collection of
data in one variable. It is contagious memory location which store alot of
data

Syntax:

For one dimensional array:

Datatype arrayname [array size];

Int a[20];

For two dimensional array:

Datatype arrayname [array size][array size];

Int a[3][4]

Where

[3] shows number of rows

[4] Shows number if column

Types:

There are two types of arrays


One dimensional arrays

1) Two dimensional arrays

Example:

#include <iostream>

#include<conio.h>

using namespace std;

int main()

int i;

int value[6] = {5,10,15,20,25,30};

cout<<"Single Dimensional Array In C++ Example Program\n";

for (i=0;i<6;i++)

cout<<"Position : "<<i<<" , Value : "<< value[i]<<endl;

return 0;

QUESTIONJ NUMBER 3(B):

Write a program to welcome your friend by getting his/her name from


keyboard using character array

Answer:

#include <iostream>

#include <string>

using namespace std;

int main ()
{

char question1[] = "What is your name? ";

string question2 = "Where do you live? ";

char answer1 [80];

string answer2;

cout << question1;

cin >> answer1;

cout << question2;

cin >> answer2;

cout <<"Hello,i am "<< answer1;

cout <<" from "<< answer2 <<"!\n";

return 0;

QUESTION NUMBER 4(A):

Write the various string function in C/C++ by using programs

Answer:

There are various types of string function in C++

For example

1. strcpy(s1, s2);
Copies string s2 into string s1.
2. strcat(s1, s2);
Concatenates string s2 onto the end of string s1.
3. strlen(s1);
Returns the length of string s1.
4. strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if
s1>s2.
5. strchr(s1, ch);
Returns a pointer to the first occurrence of character ch in string s1.

6. strstr(s1, s2);
Returns a pointer to the first occurrence of string s2 in string s1.
For example:
#include <iostream>
#include <string>
using namespace std;
int main () {
string str1 = "Hello";
string str2 = "World";
string str3;
int len ;
str3 = str1;
cout <<"str3 : "<< str3 << endl;
str3 = str1 + str2;
cout <<"str1 + str2 : "<< str3 << endl;
len = str3.size();
cout <<"str3.size() : "<< len << endl;
return 0;
}
QUESTION NUMBER 4(B)
Discuss the difference between string and character array
Answer
Strings and char array:
The thing between the double quotes (“ ”) is known as character string. In
C programming character strings are written in double quotes
A String is an abstraction, but of a sequence of characters. It says nothing
of implementation. If you wanted to create a String implementation based
on a linked list of characters there's nothing stopping
you.In a language such as C, there is very little difference - just that a c string
is a pointer to a null-terminated series of characters at sequential addresses.
While dealing with words and sentences, we actually make use of character
arrays. Up to now, we were dealing with integer arrays and storing integer
values. Here we have to see what needs to be done for storing a name. A
simple variable can't be used to store a name (which is a string of characters)
as a variable stores only a single character. We need a character array to
grab a name. A character array is not different from an integer array

QUESTION NUMBER 5(A)


Why pointers are used in program
Answer:
Pointers are a special type of variables in which a memory address is stored.
They contain a memory address, not the value of the variable.
Declaration of Pointers Pointers work by pointing to a particular data type.
We can have pointer to an integer, pointer to a double, pointer to a
character and so on.
syntax of declaring a pointer is:
data type *name ;
int *myptr;
The declaration of multiple pointers requires the use of * with each
variable name. This is evident from the following example which declares
three pointers.
int *ptr1, *ptr2, *ptr3 ;
For example:
#include <iostream>

using namespace std;

int main ()

int numbers[5];

int * p;

p = numbers;

*p = 10;

p++;

*p = 20;

p = &numbers[2];

*p = 30;

p = numbers + 3;

*p = 40;

p = numbers;

*(p+4) = 50;

for (int n=0;

n<5; n++)

cout << numbers[n] <<endl;

return 0;

}
QUESTION NUMBER 5(B)

Write a program to determine the concept of pointers and arrays. Can arrays
be used as pointers

Answer:

#include <iostream>

using namespace std;

int main()

float arr[5];

float *ptr;

cout <<"Displaying address using arrays: "<< endl;

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

cout <<"&arr["<< i <<"] = "<<&arr[i] << endl;

ptr = arr;

cout<<"\nDisplaying address using pointers: "<< endl;

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

cout <<"ptr + "<< i <<" = "<< ptr + i << endl;

return 0;

}
Why pointers are used in programs

Yes arrays be used as pointers When we write int x, it means that we have
attached a symbolic name x, at some memory location. Now we can use x
= 10 which replaces the value at that memory location with 10. Similarly
while talking about arrays, suppose an array as int y[10]. This means that
we have reserved memory spaces for ten integers and named it
collectively as y. Now we will see what actually y is? 'y' represents the
memory address of the beginning of this collective memory space. The
first element of the array can be accessed as y[0]. Remember arrays index
starts from 0 in C language, so the memory address of first element i.e.
y[0] is stored in y. “The name of the array is a constant pointer which
contains the memory address of the first element of the array” The
difference between this and an ordinary pointer is that the array name is a
constant pointer. It means that the array name will always point to the
start of the array. In other words, it always contains the memory address
of the first element ofthe array and cannot be reassigned any other
address. Let's elaborate the point with the help of following example. int
y[10]; int *yptr; In the above statements, we declare an array y of ten
integers and a pointer to an integer i.e. yptr. This pointer may contain a
memory address of an integer. yptr = y; This is an assignment statement.
The value of y i.e. the address of the first element of the array is assigned
to yptr. Now we have two things pointing to the same place, y and yptr.
Both are pointing to the first element of the array. However, y is a
constant pointer and always points to the same location whereas yptr is a
pointer variable that can also point to any other memory address.

Potrebbero piacerti anche