You are on page 1of 6

DEPARTMENT OF TELECOMMUNICATION ENGINEERING

MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO


INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 9
ST

________________________________________________________________________

Name: _____________________________________________ Roll No: _____________

Score: ____________Signature of the Lab Tutor: _____________ Date: _---------------


________________________________________________________________________

To Familiarize with Arrays & Strings in C++

PERFORMANCE OBJECTIVE
Upon successful completion of this experiment, the student will be able to:
becoming familiar with Arrays & Strings in C++ and its usage in developing program
modules.

Definitions:

C String - A null, '\0', terminated sequence of characters stored in a char array in


the C and C++ language.

C++ String - A string data type (C-like in nature) defined by the string library.

Array - A collection of individual values, all of the same data type, stored in
adjacent memory locations.

Multi-dimensional Array - An array with a multiple indexes.

Strings
The C++ language uses both C strings and C++ strings. Simply stated: C strings are
"limited" char arrays while C++ strings are unlimited char arrays with advanced
functionality. This advanced functionality will be demonstrated in the first assignment of
this lab. C++ strings are defined in the following ways below:
// C String Declaration
char message[5] = { 'H', 'E', 'L', 'P', '\0'};
// or
char message[5] = "HELP";

// C++ String Declaration (Must have #include <string> in the header.)


string message = "HELP";

The standard C++ library provides a data type (string class) that is a complement to C
string. This library is responsible for providing the definition of the C++ string as well as
string routines that provide a large assortment of string operations.
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 9
ST

________________________________________________________________________
Multi-dimensional Arrays

An array is a collection of individual values, all of the same data type, stored in adjacent
memory locations. Using the array name together with an integral valued index in
square brackets refers to the individual values. Multi-dimensional arrays have
additional indices for each additional dimension. The index indicates the position of the
individual component value within the collection of values. The index is also called the
subscript. In C++, the first array element always has subscript 0. The second array
element has subscript 1, etc. The base address of an array is its beginning address in
memory.

Declaring a Multi-dimensional Array:

In order to declare an m-dimensional array, a C++ programmer will use the following
syntax below. (m = 2)

DataType
ArrayName[ConstIntExpression][ConstIntExpression];

The example below shows the declaration of a 2-dimensional integer array of size 3 x 3
with elements [0 - 2] [0 - 2].

const int MAXSIZE = 3;


int array[MAXSIZE][MAXSIZE];

multi-dimensional array can be initialized during declaration by equating the array to a


listing of the array's members in brackets. For example: (Note: Each dimension has its
own { } brackets.)

int array[MAXSIZE][MAXSIZE] = {{2 , 4, 6}, {8, 10, 12}, {14,


16, 18}};

Passing Multi-dimensional Arrays as Function Parameters

In C++, arrays are always passed by reference. Whenever an array is passed as a


parameter, its base address is sent to the called function.
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 9
ST

________________________________________________________________________
Generally, functions that work with arrays require 2 items of information as actual
parameters: the beginning address of the array (in memory), and the number of
elements to process in the array. When passing multi-dimensional arrays as function
parameters, all subscripts except the first must contain a value of the size of that
particular index.

Below is an example of the function that prints a 2-dimensional array. (Note: The
second subscript has a value.)

For example (Function PrintArray):

void PrintArray(const int Array[][10], int ArraySize)


{
for (int row = 0; row <= ArraySize - 1; row++) {
for (int col = 0; col <= 9; col++) {
cout << "array[" << row << "][" << col << "] = ";
<< Array[row][col] << endl;
}
cout << endl;
}

Assignment:
What to Turn In: For both problems, turn in the program listing and the output. Include
the standard header in each program.

1) Complete and run the following program.

// strstuff.cpp
#include <iostream>
#include <string>

using namespace std;

void print_forward(string s);


void print_reverse(string s);

int main()
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 9
ST

________________________________________________________________________
{
string sentence, first, last;

cout << "Enter your first name: ";


cin >> first;

cout << "Enter your last name: ";


cin >> last;

sentence = "Your whole name is ";


sentence += first + " " + last + ".";

cout << endl


<< "This sentence was created by used string concatenation:"
<< endl
<< sentence << endl;

for (int i = 0; i <= sentence.length() - 1; i++)


sentence[i] = toupper(sentence[i]);

cout << endl


<< "This is the same sentence printed in upper case letters:"
<< endl
<< sentence << endl << endl;

cout << "The length of this sentence is " << sentence.length() << '.'
<< endl;

cout << endl


<<"This is the sentence printed one character at time." << endl;
print_forward(sentence);
cout << endl;

cout << endl


<<"This is the sentence printed backwards." << endl;
print_reverse(sentence);
cout << endl;

return 0;

}
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 9
ST

________________________________________________________________________

void print_forward(string s)
{
for (int i = 0; i <= s.length(); i++)
cout << s[i];
cout << endl;
}

void print_reverse(string s)
{
// Complete the code
}

2) Complete and run the following program. This program creates and prints the
multiplication table for 0 to 9.

//mtable.cpp
#include <iostream>
#include <iomanip>

using namespace std;

const int MAX = 10;

void create_table(int table[][MAX], int size);


void print_table(const int table[][MAX], int size);

int main()
{
int mtable[MAX][MAX];

create_table(mtable, MAX);
print_table(mtable, MAX);

return 0;
}

void create_table(int table[][MAX], int size)


DEPARTMENT OF TELECOMMUNICATION ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 9
ST

________________________________________________________________________
{
for (int row = 0; row <= size - 1; row++)
for (int col = 0; col <= size -1; col++)
table[row][col] = row * col;
}

void print_table(const int table[][MAX], int size)


{
// Complete the code
}

Lab Tasks:

1. Write a program that simulates the rolling of two dies. The sum of the two values
should then be calculated and placed in a single-subscripted array. Print the array.
Also find how many times 12 appear.
2. Write a C++ program to generate 20 random numbers in the range of -1 to 55 and
store them in an array named arrayNums . Then have your program find the
largest random number stored in the array.
3. Write a C++ program to output "HELLO C++ WORLD" to the screen using cout.
cout is a class contained in the file iostream.h in the standard include library.
Make use of arrays and strings.
4. Add to your program the ability to read in your name and print it out again using
cin and cout. cin is also contained in iostream.h. This program should use the
standard string manipulation functions found in string.h
i.e. strlen() and strcpy() and as well as dynamic memory allocation using new. e.g.
string_length = strlen(input_string); name_string = new char[string_length + 1];
strcpy(name_string, input_string);

You might also like