Group Assignment C++ II
Group Assignment C++ II
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*/
#include <iostream>
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; }
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; }
#include <iostream>
enum MonthsOfTheYear
January, February, March, April, May, June, July, August, September, October, November, December
};
static const char* month_names[12] = { "January", "February", "March", "April", "May", "June", "July",
int main()
_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>
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++)
{