Professional Documents
Culture Documents
Solution To Source Code For Introduction To Computing (Solution For Laboratory Exercises 5-7)
Solution To Source Code For Introduction To Computing (Solution For Laboratory Exercises 5-7)
Solution To Source Code For Introduction To Computing (Solution For Laboratory Exercises 5-7)
return 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;
}
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--;
}
//------------------------------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;
}
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";
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;
strcat(string_3, string_1);
strcat(string_3, string_4);
strcat(string_3, string_2);
#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()
{
//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
//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++)
{
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;
}
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)
{
mat1[i][j]=x[i];
}
else{
mat1[i][j]=mat1[i][j];
}
}
cout<<endl;
}
return 0;
}
//============================== (7.1-7.3)
#include <iostream>
#include <string>
using namespace std;
};
int main() {
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;
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;
}