0% found this document useful (0 votes)
2 views15 pages

C++ Chapter 5 -7 Assignment - solution

The document contains a series of C++ programming examples focusing on arrays, structures, and pointers. Each example illustrates different functionalities such as array manipulation, structure creation, and pointer usage. It serves as a practical guide for understanding fundamental programming concepts in C++.

Uploaded by

agetachewseid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views15 pages

C++ Chapter 5 -7 Assignment - solution

The document contains a series of C++ programming examples focusing on arrays, structures, and pointers. Each example illustrates different functionalities such as array manipulation, structure creation, and pointer usage. It serves as a practical guide for understanding fundamental programming concepts in C++.

Uploaded by

agetachewseid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

C++ / fundamental of programming ii Assignment

Chapter -5 - Arrays
Example 1
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
void main ()
{
clrscr();
int num[10];//array declaration an integer array n with size 10
for (int i=0;i<10;i++)
{
cout<<"enter ten natural numbers\n";
cin>>num[i];
}
cout<<"now displaying the first ten natural numbers";
cout<<setw(4)<<num[i];
getch();
}

Example 2
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
void main ()
{
clrscr();
int num[10];//array declaration an integer array n with size 10
int sum=0;
float Avg;
cout<<"please enter ten natural numbers\n";
for(int i=0;i<10;i++)
{
cin>>num[i];
sum+=num[i];//sum=sum+num[i];
}
cout<<"sum="<<sum<<endl;
cout<<"Avg="<<Avg<<endl;
getch();
}

1
C++ / fundamental of programming ii Assignment

Example 3
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
int Adama[]={16,2,77,40,12071};
int n,result=0;
int main()
{
clrscr();
for (n=0;<5;n++);
{
result+=Adama[n];//can be written as result =result+Adama;
}
cout<<"the sum of the array elements="<<result;
return 0;

Example 4
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
void main()
{
clrscr();
int num[10]={10,20,50,70,75,60,80,95,55,100}
float sum=0;
float av;
for(int i=0;i<10;i++)
{
sum+=num[i];
}
av=sum/10;
cout<<"sum of the array list="<<sum<<endl;
cout<<the average is\t"<<av;
getch();
}

Example 5
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
int main()
{
clrscr();
//we know that we need a constant number of elements consist
int max=10;
int number[max];//we will calculate thier sum
int sum =0;
cout<<"please type 10 integers\n";
for(int i=0;i<max;i++)
{
cout<<"number"<<i+1<<":";
cin>>number[i];
sum+=number[i];
}
cout<<"\n the sum of array elements are:"<<sum<<endl;
getch();
return 0;
}

2
C++ / fundamental of programming ii Assignment

Example 6
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
int main()
{
clrscr();
//declare the member of the array
int numbers[]={18,25,36,44,52,60,75,89};
int find;
int i,m=8;
cout<<"enter a number to be searched:";
cin>>find;//one of the members ofthe array elements.
for(i=0;(i<m)&&(numbr=ers[i]!=find);++i
continues;//find whether the number typed is member of the array or not
if(i==m)
cout<<find<<"is not the list"<<endl;
else
cout<<find<<"is the"<<i+1
<<"the element in the list"<<endl;
getch();
return 0;
}

Example 7
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
int main()
{
clrscr();
//the member of the array
int numbers[]={18,25,36,44,52,60,75,89};
int minimum =numbers[0];
int a=8;
//compare the members
for (int i=1;i<a;++i)
{
if(numbers[i]<minimum)
minimum=numbers[i];
}
//anounce the result
cout<<"the lowest number value of the array is"
<<minimum<<"."<<endl;
return 0;
}

3
C++ / fundamental of programming ii Assignment

Example 8
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
int main()
{
clrscr();
//the member of the array
int numbers[]={8,25,36,44,52,60,75,89};
int maximum =numbers[0];
int a=8;
//compare the members
for (int i=1;i<a;++i)
{
if(numbers[i]<maximum)
maximum=numbers[i];
}
//anounce the result
cout<<"the highest number value of the array is"
<<maximum<<"."<<endl;
return 0;
}

Example9
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
void DisplayTheArray(double mbr[], int count);
int main()
{
double distance[] = {44.14, 720.52, 96.08, 468.78, 6.28, 68.04, 364.55, 6234.12};
// Displaying first 5 members of the array
cout << "First 5 members of the array:\n";
DisplayTheArray(distance, 5);

// Processing all members of the array


int sizeOfArray = sizeof(distance)/sizeof(double);
cout << "\nAll members of the array:\n";
DisplayTheArray(distance, sizeOfArray);

return 0;
}
void DisplayTheArray(double member[], int counter)
{
for(int i = 0; i < counter; ++i)
cout << "Distance " << i + 1 << ": " << member[i] << endl;
}

4
C++ / fundamental of programming ii Assignment

Example 10
#include <iostream.h>
#include <iomanip.h>
using namespace std;

int main()
{
const int rows = 3;
const int columns = 4;
int seasonTemp[rows][columns] = {{26, 34, 22, 17}, {24, 32, 19, 13}, {28, 38, 25, 20}};
int highest = 0;

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


for (int j = 0; j < columns; ++j) {
if (seasonTemp[i][j] > highest) {
highest = seasonTemp[i][j];
}
}
}

cout << "The highest temperature is: " << highest << endl;

return 0;
}

Example 11
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
void main()
{
clrscr();
float sales[5][3];
for(int s=0;s<5;s++)
for(int m=0;m<3;m++)
{cout<<"enter sales for station"<<s+1;
cout<<"month"<<m+1<<" ";
cin>>sales[s][m];
}
for(int j=0;j<=4;j++)
for (int k=0;k<=2;k++)
{cout<<"sales"<<j+1<<"month"<<k+1;
cout<<setw(4)<<sales[j][k];
if(k==2)
cout<<"\n";
}
getch();
}

5
C++ / fundamental of programming ii Assignment

Example 12
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
void main()
{
clrscr();
char name[20];
cout<<"enter your name\t";
cin>>setw(20)>>name;
cout<<name;
getch();
}

Example 13
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
char str[20];
cout<<"enter full name\t";
cin.get(str,20);
cout<<"your name and fathers name is\n"<<str;
}

Example 14
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
{char str[100];
cout<<"enter a string and $when finish\n";
cin.get(str,100,'$');
cout<<"you entered\n"<<str;
getch();
}

6
C++ / fundamental of programming ii Assignment

Example 15

#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
char name[5][10]={"kebede","ayele","tufa","almaz","kassa"};
for(int i=0;i<5;i++)
cout<<name[i]<<'\n';
}

Example 16
#include <iostream.h>
#include <string.h>
#include <conio.h>
void main()
{
clrscr();
char name[25]
cout<<"enter your name";
cin>>name;
cout<<"your name is "<<strlen(name)<<"characters long";
getch();
}

Example 17
#include <iostream.h>
#include <string.h>
#include <conio.h>
void main()
{
clrscr();
const int size=100;
char str1[size];
char str2[size];
cout<<"enter a string\n";
cin.get(str1,size);
strcpy(str2,str1);
cout<<"copy of your string is\n"<<str2;
getch();
}

7
C++ / fundamental of programming ii Assignment

Example 18

#include <iostream.h>
#include <string.h>
#include <conio.h>
void main()
{
clrscr();
char ch1[50],ch2[100]="I am proud";
cout<<"enter a string to concatenate:";
cin.get(ch1,50);
strcat(ch1,ch2);
cout<<"the concatnated is"<<ch2;
getch();
}

Example 19

#include <iostream.h>
#include <conio.h>
#include <string.h>
void main()
{
clrscr();
const int p=15;
char ch1[p],ch2[p];
cout<<"enter first string:";
cin>>ch1;
cout<<"enter second string:";
cin>>ch2;
if(strcmp(ch1,ch2)==0)
cout<<"the two words are the same";
else if(strcmp(ch1,ch2)>0)
cout<<ch2<<"comes first alphabetically";
else if(strcmp(ch1,ch2)<0)
cout<<ch1<<"comes first alphabetically";
getch();
}
Example 20
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
void main()
{
clrscr();
char name[20];
cout<<"enter your name\t";
cin>>setw(20)>>name;
cout<<name;
getch();
}

8
C++ / fundamental of programming ii Assignment

Chapter 6-Structures
Example 1
#include <iostream.h>
#include <conio.h>
struct student{
char name[20];
char id_num[11];
int age;
};
void main()
{
clrscr();
student stud1;
cout<<"enter name of the student:";
cin>>stud1.name;
cout<<"id number:";
cin>>stud1.id_num;
cout<<"enter age:";
cin>>stud1.age;
cout<<"\n name:"<<stud1.name;
cout<<"\n id number:"<<stud1.id_num;
cout<<"\n age:"<<stud1.age;
getch();
}

Example 2

#include <iostream.h>
#include <string.h>
#include <conio.h>
struct movies_t{
char title[60];
int year;
}mine,yours;
void printmovie(movies_t movie);
int main()
{
clrscr();
strcpy(mine.title,"2001 A space Odyssey");
mine.year=1968;
cout<<"enter your favourite film title:\n";
cin.get(yours.title,60);
cout<<"enter year:\n";
cin>>yours.year;
cout<<"\n my favourite movie is :\n";
printmovie(mine);
cout<<"and yours is:\n";
printmovie(yours);
return 0;
}
void printmovie(movies_t movie)
{
cout<<movie.title;
cout<<"("<<movie.year<<")\n";
}

9
C++ / fundamental of programming ii Assignment

Example 3

#include<iostream> // use angle brackets instead of quotes for standard headers


#include<conio.h>
const int NAME = 25;
const int ID = 11;
struct student
{
char name[NAME];
char id_num[ID];
int age;
};
int main()
{
clrscr();
student stud1 = { "Alem", "019/97", 25 };
student stud2;
stud2 = stud1;
cout << "Name: " << stud1.name;
cout << "\nID: " << stud1.id_num;
cout << "\nCopied data: " << endl;
cout << "Name: " << stud2.name;
cout << "\nID: " << stud2.id_num;
cout << "\nAge: " << stud2.age;
getch();
return 0;
}
Example4
#include<iostream.h>
#include<conio.h>
struct person
{
string name;
int age;
// This is a member function that prints out the values of the member variables
void print()
{
cout << name << ";" << age << endl;
}
};
int main()
{
// Create two instances of the person struct
person a, b;
// Set the member variables of each instance
a.name = "Wales";
b.name = "Jones";
a.age = 30;
b.age = 20;
// Print out the member variables of each instance using cout statements
cout << a.name << ": " << a.age << endl;
cout << b.name << ": " << b.age << endl;
// Call the print() member function for each instance
a.print();
b.print();
return 0;
}

10
C++ / fundamental of programming ii Assignment

Example 5
#include <iostream.h>
#include <string.h>
#define N=50;
struct movies_t{
char title[60];
int year;
} user,title;
void printmovie(movies_t movie);
int main()
{
for(int n=0;n<50;n++)
{
cout<<"enter title:";
cin.get(user.title,60);
cout<<"enter year:";
cin>>user.year;
}
cout<<"\n you have entered these movies:\n";
for(n=0;n<50;n++)
printmovie;
return 0;
}
void printmovie(movies_t movie)
{
cout<<movie.title;
cout<<"("<<movie.year<<")\n";
}
Example 6
//pointers to structures
#include <iostream.h>
#include <string.h>
struct movies_t{
char title[50];
int year;
};
int main()
{
movies_t amovie;
movies_t*pmovie;
pmovie=&amovie;
cout<<"enter title:";
cin.get(pmovie->title,50);
cout<<"enter year:";
cin>>pmovie->year;
cout<<"\nyou have entered :\n";
cout<<pmovie->title;
cout<<"("<<pmovie->year<<")\n";
return 0;
}

11
C++ / fundamental of programming ii Assignment

Example7

#include <iostream.h>
#include <conio.h>
const int STR = 30;
struct address {
char street[STR];
char post[STR];
float distance; // in kilometers
};
struct employee {
address branch;
address home;
};
int main() {
employee emp1;
// employee branch address
cout << "Enter branch street of the employee: ";
cin.get(emp1.branch.street, STR);
cin.ignore(); // to consume the newline character left in the input stream
cout << "Enter branch post address: ";
cin.get(emp1.branch.post, STR);
cout << "Enter distance of the branch: ";
cin >> emp1.branch.distance;
// employee home address
cout << "Enter home street of the employee: ";
cin.ignore(); // to consume the newline character left in the input stream
cin.get(emp1.home.street, STR);
cout << "Enter home post address: ";
cin.get(emp1.home.post, STR);
cout << "Enter distance of the home: ";
cin >> emp1.home.distance;
// output employee branch address information
cout << "Employee's branch street is: " << emp1.branch.street << " and it is "
<< emp1.branch.distance << " kms away." << endl;
// wait for user input before exiting
cin.ignore(); // to consume the newline character left in the input stream
cout << "Press any key to exit." << endl;
getch();
return 0;
}

12
C++ / fundamental of programming ii Assignment

Chapter 7 - Pointers
Example 1
#include<iostream.h>
void main ( )
{
int num1=10,num2=20;
int *p;
p=&num1;
cout<<p<<endl; // displays address of num1.
cout <<*p<<endl; // displays contents of the address of num1(10)
*p=15; // changing the contents of the address in p
cout<<num1<<endl; // displays 15 (not 10)
p=&num2; // pointer points to num2.
cout<<*p<<endl; // displays contents of address (20)
*p=120; // assigning 120 to the contents of the address in p.
cout<<num2; // displays 120 (not 20)
}

Example 2
#include<iostream.h>
void main()
{
int my_var=10;
int *ptr;
ptr = &my_var;
cout << ptr; // displays the address of my-var
cout << &ptr;
}

Example 3

13
C++ / fundamental of programming ii Assignment

#include<iostream.h>
#include<conio.h>
int *iptr;
char *cptr;
float *fptr;
void main()
{
clrscr();
cout<<"size of integer pointer is :"<<sizeof(iptr)<<endl;
cout<<"size of character pointer is :"<<sizeof(cptr)<<endl;
cout<<"size of float pointer is :"<<sizeof(fptr);
getch();
}

Example 5
#include <iostream.h>
int main ()
{
int first, second;
int * mypointer;
mypointer = &first;
*mypointer = 10;
mypointer = &second;
*mypointer = 20;
cout << "first value is " << first << endl;
cout << "second value is " << second << endl;
return 0;
}

Example 7

#include <iostream.h>
void main()
{
int a[10];
cout <<"Enter the elements of the array:";
int sum = 0;
for (int i=0; i<10; i++)
{
cout<<"Enter the element :";
cin>>a[i];
sum += a[i];
}
cout<< " Sum of all the elements of the array is :"<< sum;
}

Example 10
#include <iostream.h>

14
C++ / fundamental of programming ii Assignment

int main()
{
int Number;
int number[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
cout << "An integer occupies " << sizeof(int) << " bytes\n";
cout <<"\n Number: "<<Number;
cout << "\n&number[0]: " << &number[0] << endl;
cout << "\n Number+1: " << Number+1;
cout << "\n&Number:[1] " << &number[1] << endl;
cout << "\n Number+2: " << Number+2;
cout << "\n&Number:[2] " << &number[2] << endl;
return 0;
}

15

You might also like