Professional Documents
Culture Documents
Itp Lab 9 PDF
Itp Lab 9 PDF
________________________________________________________________________
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 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.
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";
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.
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].
________________________________________________________________________
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.)
Assignment:
What to Turn In: For both problems, turn in the program listing and the output. Include
the standard header in each program.
// strstuff.cpp
#include <iostream>
#include <string>
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 << "The length of this sentence is " << sentence.length() << '.'
<< 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>
int main()
{
int mtable[MAX][MAX];
create_table(mtable, MAX);
print_table(mtable, MAX);
return 0;
}
________________________________________________________________________
{
for (int row = 0; row <= size - 1; row++)
for (int col = 0; col <= size -1; col++)
table[row][col] = row * col;
}
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);