Solution To Source Code For Introduction To Computing (Solution For Laboratory Exercises 5-7)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Page 1 of 14

Source Code for Introduction to Computing


(ECEG-2207)
Laboratory Exercises
/*
Note:
This code is generated to support the laboratory session of the course. So, each student is expected to try all
exercises by himself/herself and use this material as a reference only. In addition each student is expected to
develop a better algorithm for some of the problems. Lastly what everyone should understand is almost all
exercises are done in main function which makes the code unorganized. But this is done intentionally because
beyond the introductory portion the details of function concept has not covered till the preparation of this
document. So, you (students) are expected to organize the codes using functions after the details of the function
concept is discussed in the class
I hope you’re enjoying the course!
Good Luck!
*/
//=========================== 5.6. ====================================
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
/* This is one way of implementation. There are several ways to implementation. */
int main()
{
long double product_accumulator=1;
int counter=1;
unsigned long int product_check=1;
while(true)
{
product_check++;
if (product_check%7==0)
{
cout<<product_check<<" * ";
product_accumulator*= product_check;
counter++;
}
else
continue;
if(counter==1000)
break;
}
cout<< "\n\nThe product of the first 1000 number which are multiple of 7
are"<<fixed<<product_accumulator<<endl;

return 0;
}

Addis Ababa University/AAiT By Hailemelekot D. Dec 2014GC


Page 2 of 14

//===================== 5.4 =============================


#include<iostream>
#include<cmath>
using namespace std;
// Function for determining the first n primes
int is_prime(int i)
{
int x=0;

if(i==2)
{
x=1;
}
for(int n=2;n<i;n++)
{

if((i%n)==0)
{
x=0;
break;
}
else
{
x=1;
}
}
return x;
}

int main()
{
int count=0;
int i=0;
int sum_acc=0;
while(count<1000)
{
//cout<<"hi";
i++;
if(is_prime(i)==1)
{
cout<<"\t"<<i<<"+";
sum_acc+=i;
count++;
}
else
{
continue;
i++;
}
}
cout<<"\nThe sum of the first "<<count<<" prime numbers is: "<<sum_acc;
return 0;
}

Addis Ababa University/AAiT By Hailemelekot D. Dec 2014GC


Page 3 of 14

//============================ 5.2 ====================================


#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n;
//variable initialization
int x=123;
int z=100;

int y=0;
cout<<"Enter the size of number that u want to reverse\n";
cin>>n;
cout<<"Enter the number that u want to reverse";
cin>>x;
z=pow(10.0,(n-1));
while(n>0)
{
y+=(x%10)*z;
x/=10;
z/=10;
n--;
}

cout<<"\nThe reversed integer is :"<<y;


return 0;
}

//======================== 5.3 (The three patterns) =====


//------------------------------Pattern 2
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
for (int i = -5; i < 6; i++)
{
for (int j = -5; j < 6; j++)
{
if (abs(i) >= abs(j))
{
cout << "#";
}
else
{
cout << " ";
}
}
cout << endl;
}
return 0;
}

Addis Ababa University/AAiT By Hailemelekot D. Dec 2014GC


Page 4 of 14

//------------------------------Pattern 1
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
for (int i = -5; i < 6; i++)
{
for (int j = -5; j < 6; j++)
{
if (abs(i) == abs(j))
{
cout << "#";
}
else
{
cout << " ";
}
}
cout << endl;
}
return 0;
}
//--------------------------------------Pattern 3
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
for (int i = -5; i < 6; i++)
{
for (int j = -5; j < 6; j++)
{
if (abs(i) == abs(j)||j==-5||j==5)
{
cout << "#";
}
else
{
cout << " ";
}
}
cout << endl;
}
return 0;
}

Addis Ababa University/AAiT By Hailemelekot D. Dec 2014GC


Page 5 of 14

//========================================== 6.1. ======================


#include<iostream>
#include<cmath>
using namespace std;
int main()
{
float a[20];
float _sum=0;
int n=0;
if(n<0||n>20)
{
cout<<"invalid input";
exit(0);
}
cout<<" Enter the size of the binary number\n";
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
if(a[i]<0||a[i]>0)
{
cout<<"invalid input";
exit(0);
}

for(int i=0;i<n;i++)
{
cout<<a[i];
}
cout<<endl;

for(int i=0;i<n;i++)
{
_sum+=( a[i]*(pow(2.0,(n-1)-i)));
}
cout<<endl<<_sum;
}
//============================ 6.5======================
#include<iostream>
#include<cmath>
#include<string>
#include<cstring>
using namespace std;
int main()
{
char i[]="We offer a wide range of courses at all levels. If you are applying through one of our representatives in
your country, they will guide you through making your application.";
char k[]="apply";
char h[]="country";

Addis Ababa University/AAiT By Hailemelekot D. Dec 2014GC


Page 6 of 14

cout<<strstr(i,k);
cout<<endl;
cout<<strstr(i,h);
cout<<endl;

return 0;
}
//=================================== 6.6 ============
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1;
string s2;
cout<<"Inter the first string: \n";
cin>>s1;
cout<<"Inter the second string: \n";
cin>>s2;
cout<<"The sorted strings are: \n";
if(s1<s2)
{
cout<<s1<<endl;
cout<<s2<<endl;
}
else if (s1>s2)
{
cout<<s2<<endl;
cout<<s1<<endl;
}
else
{
cout<<s1<<" "<<s2;
}
return 0;
}
//=============================6.4 =================================
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char string_1[5];
char string_2[5];
char string_3[10];
char string_4[]=" ";
cout<<"Inter the first string : ";
cin>>string_1;
cout<<"Inter the second string: ";
cin>>string_2;

Addis Ababa University/AAiT By Hailemelekot D. Dec 2014GC


Page 7 of 14

strcat(string_3, string_1);
strcat(string_3, string_4);
strcat(string_3, string_2);

cout<<"The concatinated string is: \n";


cout<<string_3;
return 0;
}
//============================= 6.3=======================

#include<iostream>
#include<string>
/*
This is one way of implementation. It can be implemented using several ways:
for instance you can use strlen() function by taking the strings as a character
arrays from the keyboard or even you can just count the number of characters using loops
and do the same thing:
Remember: In programing profession one problem may have several solutions
*/
using namespace std;
int main()
{
string name;
cout<<"Inter your name: ";
cin>>name;
if(name.length()>10)
{
cout<<"Your name is too long!";
}
else if(name.length()>=5 && name.length()<=10)
{
cout<<"Your name has medium length!";
}
else
{
cout<<"Your name is too short!";
}
return 0;
}
//=============================6.2 (a-f)=================================
/*========================================================
The only way to know programming is just programming
This code shall be used as a reference after you tried your own code
Just try to to find other better solution for the problems beyond these codes
not only for this questions but also for other exercises
=========================================================*/
#include<iostream>
#include<cmath>
using namespace std;

int main()

Addis Ababa University/AAiT By Hailemelekot D. Dec 2014GC


Page 8 of 14

{
//decleration of 2d arrays for matrix operation
double mat1[3][3];
double mat2[3][3];
double _mat_sum[3][3]; //to sore the sum of mat1 and mat2
double _mat_prod[3][3]; //to store the sum of mat1 and mat2
double _diago_exch[3][3];
double mat3[3][3]; //to store the transpos of mat1

// declaration of of 1d matrix for holding the storing the sum of rows


// and columns of one of the matrix
double array[3];
double entire_sum=0;
double _sum=0; //To hold temporary sum of the row and column

//taking the element of the matrices from the user


cout<<"Inter the elements of the first matrices\n";
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<"Inter the elenent at: row "<<i<<" column "<<j<<" ";
cin>>mat1[i][j];
}
}

cout<<"Inter the elements of the second matrices\n";


for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<"Inter the elenent at: row "<<i<<" column "<<j<<" ";
cin>>mat2[i][j];
}
}
cout<<endl;

//6.2A adding the entire elements of the matrix (the first matrix is enough)
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
//cout<<"Inter the elenent at: row "<<i<<" column"<<j<<endl;
entire_sum+=mat1[i][j];
}
}
cout<<"\nThe sum of the entire elements of the matrix is: "<<entire_sum<<endl;
//6.2B add the rows of the matrix and store it in a single dimensional array
for(int i=0;i<3;i++)
{

Addis Ababa University/AAiT By Hailemelekot D. Dec 2014GC


Page 9 of 14

for(int j=0;j<3;j++)
{
//cout<<"Inter the elenent at: row "<<i<<" column"<<j<<endl;
_sum+=mat1[i][j];
}
array[i]=_sum;
_sum=0;
}
for(int i=0; i<3; i++)
{
cout<<"\nThe sum of the elements of the matrix one at row: "<<i<<" is: "<<array[i]<<endl;
}
//6.3 c Transposing the two matrix

for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
mat3[i][j]=mat1[j][i]; //store the transpose of mat1 on mat3
}
}
//----printing the transposed matrix at the console
cout<<"\nThe transpose of the second matrix is:\n";
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<mat3[i][j]<<" ";
}
cout<<endl;
}
//6.2 D compute the sum of the two matrices
//the program computes the sum of mat1 and mat2 and store the sum in _mat_sum

for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
_mat_sum[i][j]=mat1[i][j]+mat2[i][j];
}
cout<<endl;
}
cout<<"The sun of the two mat1 and mat2 is: \n";
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<_mat_sum[i][j]<<" ";
}
cout<<endl;
}

Addis Ababa University/AAiT By Hailemelekot D. Dec 2014GC


Page 10 of 14

//6.2 E computing the product of the two matrices;


//the program computes the product of mat1 and mat2 and store it in _mat_prod
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
for(int k=0;k<3;k++)
{
_mat_prod[i][j]+=mat1[i][k]*mat2[k][j];
}
}
cout<<endl;
}
cout<<"The product of the two matrices is :\n";
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<_mat_prod[i][j]<<" ";
}
cout<<endl;
}
double x[3]; //single dimensional arrays to store the one diagonal of mat1
double y[3]; //single dimensional arrays to store the other diagonal of mat2
for(int i=0;i<3;i++)
{ int k=0;
int l=0;
for(int j=0;j<3;j++)
{

if(i==j)
{
x[i]=mat1[i][j];
}
if((i+j)==2)
{
y[i]=mat1[i][j];
}
}
cout<<endl;
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(i==j)
{
mat1[i][j]=y[i];
}
else if((i+j)==2)

Addis Ababa University/AAiT By Hailemelekot D. Dec 2014GC


Page 11 of 14

{
mat1[i][j]=x[i];
}
else{
mat1[i][j]=mat1[i][j];
}
}
cout<<endl;
}

cout<<"\nExchange the diagonals of mat1\n";


for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<mat1[i][j]<<" ";
}
cout<<endl;
}

return 0;
}

//============================== (7.1-7.3)
#include <iostream>
#include <string>
using namespace std;

enum gender { male, female};


//structure definition
struct Employee_record
{
string f_name;
string m_name;
string l_name;
string emp_id;
gender _gender;
//-----------------
string city;
string street;
string m_phone;
string f_phone_h;
string f_phone_o;
string fax;
string email;
short int p_o_box;
//----------------------
string department;
float sallary;
unsigned short int hire_date;

Addis Ababa University/AAiT By Hailemelekot D. Dec 2014GC


Page 12 of 14

unsigned short int hire_month;


unsigned short int hire_year;
unsigned short int quit_date;
unsigned short int quit_month;
unsigned short int quit_year;
};

//Grouping related elements in structures (for 7.3)


struct Employee_name_rec{
string f_name;
string m_name;
string l_name;
string emp_id;
gender _gender;
};
struct Employee_adress_rec{
string city;
string street;
string m_phone;
string f_phone_h;
string f_phone_o;
string fax;
string email;
short int p_o_box;
};
struct hire_date_rec{
unsigned short int hire_date;
unsigned short int hire_month;
unsigned short int hire_year;
};
struct quit_date_rec{
unsigned short int quit_date;
unsigned short int quit_month;
unsigned short int quit_year;
};
//structures of structures
struct company_specific_rec{
string department;
float sallary;
hire_date_rec hdr;
quit_date_rec qdr;
};
struct Employee_rec
{
Employee_name_rec enr;
Employee_adress_rec edr;
hire_date_rec _hdr;
quit_date_rec _qdr;
company_specific_rec csr;

};

Addis Ababa University/AAiT By Hailemelekot D. Dec 2014GC


Page 13 of 14

int main() {

Employee_record myer, mr_x_er, mrs_x_er,mr_bill_gates_er;


myer={ "hailemelekot","D","T","C578",male,"Addis Ababa", "Omedla",
"+251 913 ...", "+251 ---", "---","+251 ----", "hailemelekotd@gmail.com",
123, "SECE", 0000, 1,10,2014,1,1,2015 };

//displaying my employee record(my_er elements)


cout<<myer.f_name<<endl;
cout<<myer.m_name<<endl;
cout<<myer.l_name<<endl;
cout<<myer.emp_id<<endl;
cout<<myer._gender<<endl;

cout<<myer.city<<endl;
cout<<myer.street<<endl;
cout<<myer.m_phone<<endl;
cout<<myer.emp_id<<endl;
cout<<myer.f_phone_h<<endl;
cout<<myer.f_phone_o<<endl;

cout<<myer.fax<<endl;
cout<<myer.email<<endl;
cout<<myer.p_o_box<<endl;

cout<<myer.department<<endl;
cout<<myer.sallary<<endl;

cout<<myer.hire_date<<"/";
cout<<myer.hire_month<<"/";
cout<<myer.hire_year<<endl;

cout<<myer.quit_date<<"/";
cout<<myer.quit_month<<"/";
cout<<myer.quit_year<<endl;

// Lab exercise 7.2


Employee_record arrys_of_er[10];
arrys_of_er[0]=myer;
arrys_of_er[1]=mr_x_er;
arrys_of_er[2]=mrs_x_er;
arrys_of_er[3]=mr_bill_gates_er;
// .
// .
// .
// 9
//accssing array elements
arrys_of_er[0].f_name="haile";
cout<<endl;
cout<<"First name: "<<arrys_of_er[0].f_name;
cout<<"\nPrevious Sallary: "<<arrys_of_er[0].sallary;

Addis Ababa University/AAiT By Hailemelekot D. Dec 2014GC


Page 14 of 14

arrys_of_er[0].sallary+=500;
cout<<"\nSallary After Modifications: "<<arrys_of_er[0].sallary;
//===================Structures of Structures (7.3 extended) ========================
cout<<"\nAccessing elements of the structure of structures\n";
Employee_rec emp_rec;
emp_rec._hdr.hire_date=1;
emp_rec._hdr.hire_month=10;
emp_rec._hdr.hire_year=2014;
cout<<"\nThe Employment Date: "
<<emp_rec._hdr.hire_date<<"/"
<<emp_rec._hdr.hire_month<<"/"
<<emp_rec._hdr.hire_year<<endl;
return 0;
}

Addis Ababa University/AAiT By Hailemelekot D. Dec 2014GC

You might also like