Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
REG #:br540076
GROUP: A
Answer:
#include <iostream>
int main()
int n;
cin >> n;
}
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>
int main()
++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>
int main()
int n,i,s;
i=1;
s=0;
do
s=s+i;
i++;
while (i<=n);
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
Answer:
#include<iostream>
main()
{
int a,b,sum,sub,mult,div,mod;
cin>>a;
cin>>b;
sum=a+b;
sub=a-b;
mult=a*b;
div=a/b;
mod=a%b;
Part no 1:
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:
Int a[20];
Int a[3][4]
Where
Types:
Example:
#include <iostream>
#include<conio.h>
int main()
int i;
for (i=0;i<6;i++)
return 0;
Answer:
#include <iostream>
#include <string>
int main ()
{
string answer2;
return 0;
Answer:
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
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;
n<5; n++)
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>
int main()
float arr[5];
float *ptr;
ptr = arr;
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.