INTITUTE OF
TECHNOLOGY
DEPARTMENT: SOFTWARE ENGINEERING
COURSE TITLE: Computer Programming II
GROUP: ASSIGNMENT
SECTION: B
NO NAMES ID NO
1. SHARMARKE SHAKIB AHMED EV/1321/13
2. AHMED ABDIRAHMAN MAHAMED EV/1674/13
3. MOHAMED FARHAN HOSSEN EV/1309/13
4. MOHAMED KORANE ISSE EV/1310/13
5. MOHAMED AHMED SI’ID EV/1308/13
SUB DATE: ON JANUARY 2023
Group Assignment
1.Write a function which output all prime numbers between 2 and a given positive
Integer n:
Hint:- double sum (unsigned int n); A number is prime if it is only divisible by itself and 1.
Answer
#include<iostream>
using namespace std;
void primes( int n){
int k=0;
for( int i= 2; i < n; i++){
for( int j = 2; j < i ; j++) {
if ( i % j==0)
k++;}
if ( k == 0)
cout << i << endl;
k=0;}
}
int main () {
int n ;
cout<< "enter n : " ;
cin>>n;
primes( n);
return 0; }
2. Write a function which returns the sum of a list of real values
Hint:- double Sum (int n, double val ...); where n denotes the
number of values in the list.
Answer
#include <iostream>
using namespace std;
double Sum(int n,double Val[])
{
double sum=0.0;
for(int i = 0; i<n; i++)
sum+= Val[i];
return sum;
}
int main()
{
double Val[]={2.2,4.4,5.5,6.6};
int n = sizeof(Val)/sizeof(Val[0]);
cout << "Sum ="<<Sum(n,Val);
return 0;
}
3. Write a C++ programs to find the largest and the smallest Elements in array
Hint:- the maximum size of the array is 10
Answer
#include<iostream>
using namespace std;
int main(){
int arr[10]; // declaring an array of size 10
int largest, smallest; /*declaring two variables to store largest and smallest
element*/
// initializing the variables
largest = arr[0];
smallest = arr[0];
// iterating over the array elements to find largest and smallest element
for(int i=0; i<10; i++){
if(arr[i] > largest)
largest = arr[i];
else if(arr[i] < smallest)
smallest = arr[i];
}
cout<<"Largest element : "<<largest<<endl;
cout<<"Smallest element : "<<smallest;
return 0;
}
4.The following table specifies the average seasonal temperature for three major Ethiopian
cities. Define a two-dimensional array to capture this data:
Summer Spring Winter
Jigjiga 28 22 30
Addis Ababa 20 18 25
Dire Dawa 25 28 32
Write a function which outputs this table element by element.
Answer
#include <iostream>
using namespace std;
void fun (int temper [ ] [3], const int size);
int main( ) {
const int city = 3;
const int weather =3;
int temper[city][weather]= {
// Summer Spring Winter
/*JIGJIGA*/ {28, 22, 30},
/*ADDIS ABABA*/ {20, 18, 25},
/*DIRE DAWA*/ {25, 28, 32}
};
fun (temper , city);
return 0; }
void fun (int temper [ ] [3], const int size)
{
for(int i=0; i < size; i++)
{
for(int j=0; j < 3; j++)
{
cout<< temper[i][j]<<" \t";
}
cout<< "\n" ;
}
}
5. Based on question number 4 rewrite the program in one dimensional array?
Answer
#include <iostream>
using namespace std;
void fun(int Arr[], int sizeofArray);
int main() {
// Summer Spring Winter
int jigjiga[3]= {28, 22, 30};
int Addis[3]= {20, 18, 25};
int Dire[3]= {25, 28, 32};
fun(jigjiga, 3);
fun(Addis, 3);
fun(Dire, 3);
return 0; }
void fun(int Arr[], int sizeofArray){
for(int i=0; i <sizeofArray ; i++)
{
cout << Arr[i]<<" \t";
}
cout << "\n";
}
6. Define an enumeration called Month for the months of the year and use it to
define a function which takes a month as argument and returns it as a constant
string.
Answer
#include <conio.h>
#include <iostream>
using namespace std;
enum MonthsOfTheYear
January, February, March, April, May, June, July, August, September, October, November, December
};
const char* month_to_string(MonthsOfTheYear month)
static const char* month_names[12] = { "January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December" };
int index = (int)month;
return (index < 0 || index > 11) ? "Wrong month" : month_names[index];
int main()
cout << month_to_string(March) << endl;
cout << month_to_string(November) << endl;
cout << month_to_string(July) << endl;
_getch();
return 0;
}
7. The following table specifies the major contents of four brands of breakfast
cereals. Define a two-dimensional array to capture this data:
Fiber Sugar Fat Salt
Top flake 12g 25g 16g 0.4g
Cornabix 22g 4g 8g 0.3g
Oatabix 28g 5g 9g 0.5g
Ultraban 32g 7g 2g 0.2g
Answer
#include <iostream>
using namespace std;
void fun (float breakfast [ ] [4] , const float size);
int main()
{
const int cereals =4;
const int nutri = 4;
float breakfast[cereals][nutri]=
{ // Fiber sugar fat salt
/*TOP FLAKE*/ {12, 25, 16, 0.4},
/*CORNABIX*/ {22, 4, 8, 0.3},
/*OATABIX*/ {28, 5, 9, 0.5},
/*ULTRABRAN*/ {32, 7, 2, 0.2}
};
fun (breakfast ,cereals );
return 0;}
void fun (float breakfast [ ] [4] , const float size)
{
for(int i=0; i < size; i++)
{
for(int j=0; j < 4; j++) {
cout<< " "<< breakfast [i] [j] << "g \t";
} cout<< " \n";
}