ch 5 array cjr
ch 5 array cjr
Program 4: For a two-dimensional array 3x3 find (1) sum of all elements.(2)row-wise sum. (3)Column
–wise sum.
// To sum elements row-wise.
// To find sum of matrix elements. for(i=0; i<m; i++)
#include<iostream.h> {
main() rsum = 0;
{ for(j=0; j<n; j++)
int a[10][10], i, j, sum, rsum, csum, m, n; rsum += a[i][j];
cout <<”enter the order of matrix \n”; cout<<"row number:"<<(i+1)<<"\t row sum =
cin>>m>>n; "<<rsum<<" \n";
cout<<”enter the elements of the matrix one by one \n”; }
for(i=0; i<m; i++)
for(j=0; j<n; j++) // To sum column-wise
cin>>a[ i ][j]; for(j=0; j<n; j++)
//to sum all elements of matrix. {
sum = 0; csum=0;
for(i=0; i<m; i++) for(i=0; i<m; i++)
for(j=0; j<n; j++) csum += a[i][j];
sum += a[i][j]; cout<<"column number:"<<(j+1)<<"\tcolumn
cout<<”sum of the elements of the matrix is : “<<sum; sum= "<<csum<<"\n";
}}
5.2 Strings:
A String is an array of characters i.e., they are defined between the single quotes.
A string is a character array terminated by a null character. Null character is specified as ‘ \0 ’.
So, the size should be equal to maximum on. of characters in the string plus one.
Eg: char name [5] = { ‘j’ , ‘o’, ‘n’, ‘y’, ‘\0’}
2/8
3/ 8
Declaration of string variable: char string-name [size] { Size — No. of characters in the
String-name }
Eg., char sname[30],country[40];
Reading strings: cin operator can be used to read a string eg., char name[50];
Cin>>name; — terminates when first blank character is encountered.
Thus, usually we use a new command to read entire line
cin.getline(name,50); — reads entire string until terminated by the enter key or 49
characters are read(which ever occurs first).
b) String Concatenation (strcat): This function adds 2 strings & places in the first string. I.e., the
function appends the second string to the first.
c) Copying two strings (strcpy): This will assign the contents of one string or character array to
the string variable.
Eg. strcpy(n, "Ethiopia") Stores the character array ’Ethiopia’ in string n.
strcpy(n1,n2) Stores the contents of n2 to n1 erasing the contents of
n1 if any.
d) Comparing two strings (strcmp): This function is used to compare two strings. This compares
the ASCII values of the strings.
3/8
4/ 8
e) Reversing the String (strrev): This function is used to reverse the given string.
Program. To count number of characters, words & blank spaces in the given line.
// to count no. of characters, words & blank spaces.
#include <iostream.h>
#include <conio.h>
#include <string.h>
void main()
{
int now,noc,nos,i;
char st[100];
cout<<"enter the string: ";
cin.getline(st,100);
noc=now=nos=0;
for(i=0;i<strlen(st);i++)
{
noc++;
if(st[i]==' ')
{
now++;
nos++;
noc--;
}
}
now++;
cout<<"\nno of characters "<<noc;
cout<<"\nno of words: "<<now;
cout<<"\n no of spaces: "<<nos;
getch();
}
Program 2. : To convert uppercase to lowercase & vice-versa.
#include<iostream.h>
#include<ctype.h>
#include<string.h>
main()
{
int i;
char str[50],ch=’y’;
while(ch==’y’)
{
cout<<”Enter the string to convert \n”;
4/8
5/ 8
cin.getline(str,20);
i= 0;
while(str[i]!=’\0’)
{
if(islower(str[i]))
str[i] = toupper(str[i]);
else
str[i] = tolower(str[i]);
i++;
}
cout<<”converted string is :”<<str<<”\n”;
cout<<”do U continue(y/n)? \n”;
cin>>ch;
}
}
Tutorial:
1. Write a program to extract left & right most ‘n’ Characters.[Hint : (a)Accept no. of characters from
left. Put condition up to that value & display. (b) Using strlen(),subtract the starting value of the right
most string & display]
5.3 Structures:
A Structure is a collection of data item or variables of different data types that are referred to same
name.
declaration: struct tag-name
{
data-type members;
}
struct — tells the computer structure is being defined, that may be used to create struct variable.
tag-name— identifies particular structure and its type specifier.
fields that comprise the structure are called structure elements. All elements are logically related to
each other.
char idno[10];
int maths,phy,chem.;
}I year,II year,IIIyear;
Usage of tag-name is also optional i.e., without stdrec is also valid, but does not have a tag-name for
later use.
Referencing structure elements is members must be linked to struc variables in order to make them
more meaningful. This is established through dot operator called as member operator or period
operator. for eg., Iyear.phy.
Structure initialization:
struct stdrec
{
char name[20];
char idno[10];
int maths, phy, chem;
}markrec = {“raju”,”reg 01/94”,60,70,76}; Here initial values are assigned to respective fields
correspondingly.
Array of structures:
This is most commonly used structures. To define this first the structure must be defined and then
array variable must be declared.
eg., emp empinfo[10];
This creates 10 sets of variables that are organized as defined in structure ‘emp’. array structure begin
their indexing at 0. array of structures is stored in memory as multidimensional array.
main()
{
struct empinfo
{
char name[20];
int empno,basic;
};
empinfo emp[10];
int n,i;
cout<<” Enter how many employees \n”;
cin>>n;
for(i=0; i<n; i++)
{ for(i=0; i<n; i++)
cout<<”Enter name \n”; {
cin.getline(emp[i].name,20); cout<<”\n name of employee”<<emp[i].name;
cout<<”\n Employee no. is ”<<emp[i].empno;
cout<<” Enter employee no \n”; cout<<”\n Basic salary ”<<emp[i].basic;
cin>> emp[i].empno; }
}
cout<<” Enter employee basic pay \n”;
cin>> emp[i].basic;
}
cout<<"name "<<sem.name<<endl;
for (i=0; i<2; i++)
cout<<sem.S[i]<<"\n";
getch();
}
Program to print maximum marks along with the name of the student.
#include<iostream.h>
#include<string.h>
main()
{
struct stdrec
{
char name[20];
int s1, s2, s3;
};
struct stdrec mark[5];
int i, tot[5], high;
char tname;
for (i=0; i<5; i++) // Input details
{
cout<<”\n enter name \n”;
cin.getline(mark[i].name,20);
cout<<”enter marks of 3 subjects”;
cin>> mark[i].s1>>mark[i].s2>>mark[i].s2;
8/8