PL2 Lab4
PL2 Lab4
LAB MANUAL 04
CSC 2207 Programming Language 2 [EEE]
TITLE
PREREQUISITE
OBJECTIVE
THEORY
ARRAY
An array can hold a series of elements of the same type. Arrays are a convenient way of
grouping a lot of values of same type under a single variable name. Arrays are like pigeon
holes or chessboards, with each compartment or square acting as a storage place; they can
be one dimensional, two dimensional or more dimensional.
#include <iostream>
using namespace std;
int main() {
int numbers[5];
cout << "Enter 5 numbers: " << endl;
// store input from user to array
for (int i = 0; i < 5; ++i) {
cin >> numbers[i];
}
cout << "The numbers are: ";
// print array elements
for (int n = 0; n < 5; ++n) {
cout << numbers[n] << " ";
}
return 0;
}
#include <iostream>
using namespace std;
}
int main() {
// declare and initialize an array
int marks[5] = {88, 76, 90, 61, 69};
display(marks,5);
return 0;
}
TWO DIMENSIONAL ARRAY
#include <iostream>
using namespace std;
int main() {
int numbers[2][3];
cout << "Enter numbers: " << endl;
// store input from user to array
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cin >> numbers[i][j];
}
}
cout << "The numbers are: "<<endl;
// print array elements
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cout<< numbers[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
STRING
The way a group of integers can be stored in an integer array, similarly a group of characters can
be stored in a character array. Character arrays are many a time also called strings. Character
arrays or strings are used by programming languages to manipulate text such as words and
sentences.
char name[ ] = { 'H', 'A', 'E', 'S', 'L', 'E', 'R', '\0' };
string name;
Take input name from user and print it.
#include <iostream>
using namespace std;
int main() {
char name[25];
//string name;
cout<<"Enter your name ";
cin>> name ;
cout<< "Hello "<<name;
return 0;
}
#include <iostream>
using namespace std;
int main() {
char name[25];
cout<<"Enter your name ";
gets(name);
cout<< "Hello ";
puts(name);
return 0;
}
#include<iostream>
using namespace std;
int main(){
char name[50];
gets(name);
puts(name);
string department;
//cin>>department; // cin take input one word
getline(cin,department); // can take input a line
cout<<department;
return 0;
}
With every C compiler a large set of useful string handling library functions are provided. Out of
them we shall discuss the functions strlen( ), strcpy( ), strcat( ), strcmp( ) and strrev(), since
these are the most commonly used functions.
#include<iostream>
#include<cstring> // or use #include<string.h>
using namespace std;
int main(){
cout<<strlen(department);
return 0;
}
LAB WORK
1. Take Inputs from User and Store them in an Array and Display Sum and Average of
Array Elements. Use function and array.
2. Write a program in C++ to print prime in range 1 - 100. Use function and array
3. Write a program that asks user to input his/her full name and outputs the name in reverse.
Hint: Use STRREV()
4. Write a program that asks user to input his/her two favorite subjects and after
concatenating the 1st subject at the end of the 2nd subject gives output. Hint: Use
STRCAT()
5. Write a program that stores the name of a food in a string called source but outputs the
same name in another string called target . Hint: Use STRCPY()
ASSIGNMENT
1. Write a program in C++ to find the first 10 natural numbers. Use array
Sample output:
2. Write a program in C++ to find the sum of first 10 natural numbers. use array and
function.
Sample Output:
3. Below there is an Array of positive and negative number. Write a program that calculates
how many of the numbers are positive and how many of them are negative.
4. Let us consider two 3*2 matrices A and B. Write a program that add them after taking
input for matrices A and B from the user.
5. Let us consider two matrices A(3*2) and B(2*3). Write a program that multiplies them
after taking input for matrices A and B from the user.
6. There are two resistors in a circuit. R1=four ohm and R2= four ohm. Write a program
that compares R1 and R2 to find out whether they are same or different.
7. Write a program that asks user to input his/her full name and outputs the name in reverse.
Hint: Cannot use STRREV()