You are on page 1of 7

American International University Bangladesh (AIUB)

Faculty of science & Technology


Department of Computer Science

LAB MANUAL 04
CSC 2207 Programming Language 2 [EEE]
TITLE

A Brief Introduction to Array and String

PREREQUISITE

• To know about C++ variables


• To be able to use cout and cin
• To know about decision control structure
• To know about loop control structure

OBJECTIVE

• To know why Array is needed


• To be able to store and access Array elements manually
• To be able to store and access Array elements through loops
• To know about Two Dimensional Array
• To know about strings
• To be able to declare and initialize strings
• To know about gets() and puts() function
• To know about string library functions strlen(), strcpy(), strcat(), strcmp() and strrev()
• To be able to solve the exercises, lab work and assignments at the end of this manual

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.

double gpa[40]; // declare array of size 40


gpa[0]= 3.5; // Assign value
int mark[5] = {19, 10, 8, 17, 9} // declaration and initialization same time

Take Inputs from User and Store them in an Array.

#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;
}

Array and function

#include <iostream>
using namespace std;

void display(int m[],int length) {

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


cout << m[i] << endl;
}

}
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.

DECLARING AND INITIALIZING A STRING

A string constant is a one-dimensional array of characters terminated by a null ( ‘\0’ ). For


example,

char name[ ] = { 'H', 'A', 'E', 'S', 'L', 'E', 'R', '\0' };

char name[] = “Richard”;

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;
}

GETS() AND PUTS()FUNCTION

#include <iostream>
using namespace std;

int main() {

char name[25];
cout<<"Enter your name ";
gets(name);
cout<< "Hello ";
puts(name);

return 0;
}

Character Array and string in C++

#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;
}

STANDARD LIBRARY STRING FUNCTIONS

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(){

//string department; gives you error


char department[20];
cin>>department;

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:

The natural numbers are: 1 2 3 4 5 6 7 8 9 10

2. Write a program in C++ to find the sum of first 10 natural numbers. use array and
function.

Sample Output:

The sum of first 10 natural numbers: 55

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.

Hint: Use ONE DIMENSIONAL ARRAY.

A [ ] = {10, -2, 9. -4, -6, -7, 12, 14, 19, -20}

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.

Hint: Use TWO DIMENSIONAL ARRAY

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.

Hint: Use TWO DIMENSIONAL ARRAY

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.

Hint: Cannot use STRCMP()

7. Write a program that asks user to input his/her full name and outputs the name in reverse.
Hint: Cannot use STRREV()

You might also like