c++ worksheet on function,array and structure
c++ worksheet on function,array and structure
#include <iostream>
using namespace std;
int main() {
double length = 5.0;
double width = 3.0;
cout << "Area of the rectangle: " << calculateArea(length, width) <<
endl;
return 0;
}
Output:
#include <iostream>
using namespace std;
int main() {
double celsius = 25.0;
cout << "Temperature in Fahrenheit: " << celsiusToFahrenheit(celsius)
<< endl;
return 0;
}
Main Office:-Gondar Behind PLaza Hotel Tel:-+251 918 707117/+251 953 202827
2
ANDROMEDA COMPUTER AND SOFTWARE
ACADEMY
Output:
Task: Write a function calculateInterest that takes the principal amount, rate of
interest, and time (in years) as parameters and returns the simple interest.
Hint: Simple Interest = Principal×Rate×Time
#include <iostream>
using namespace std;
int main() {
double principal = 10000.0;
double rate = 5.0;
int time = 2;
cout << "Simple Interest: " << calculateInterest(principal, rate, time) <<
endl;
return 0;
}
Output:
For a principal of 10000.0, a rate of 5.0%, and a time of 2 years, the simple interest is
1000.0.
Task: Write a function calculateBill that takes the original price and discount
percentage as parameters and returns the final price after discount.
Hint: Final Price =Original Price−(Original Price×Discount Percentage)/100
Main Office:-Gondar Behind PLaza Hotel Tel:-+251 918 707117/+251 953 202827
3
ANDROMEDA COMPUTER AND SOFTWARE
ACADEMY
#include <iostream>
using namespace std;
int main() {
double price = 250.0;
double discount = 10.0;
cout << "Final Price after discount: " << calculateBill(price, discount) <<
endl;
return 0;
}
Output:
For an original price of 250.0 and a discount of 10%, the final price is 225.0.
Part 2: Arrays
#include <iostream>
using namespace std;
int main() {
double grades[5] = {85.5, 90.0, 78.5, 88.0, 92.0};
double sum = 0.0;
for(int i = 0; i < 5; i++) {
sum += grades[i];
}
double average = sum / 5;
cout << "Average grade: " << average << endl;
return 0;
}
Output:
Main Office:-Gondar Behind PLaza Hotel Tel:-+251 918 707117/+251 953 202827
4
ANDROMEDA COMPUTER AND SOFTWARE
ACADEMY
2. Problem: Finding the Maximum Value in an Array
o Task: Write a program that finds the maximum value in an
array of 7 integers.
o Hint: Traverse the array and compare each element to find the
maximum.
#include <iostream>
using namespace std;
int main() {
int numbers[7] = {4, 7, 1, 8, 5, 9, 3};
int max = numbers[0];
for(int i = 1; i < 7; i++) {
if(numbers[i] > max) {
max = numbers[i];
}
}
cout << "Maximum value: " << max << endl;
return 0;
}
Output:
Task: Write a program that stores the prices of 5 items in an array and displays each price
along with the total sum.
Hint: Use a loop to display each item price and calculate the total sum.
#include <iostream>
using namespace std;
int main() {
double prices[5] = {19.99, 29.99, 15.99, 9.99, 49.99};
double total = 0.0;
Main Office:-Gondar Behind PLaza Hotel Tel:-+251 918 707117/+251 953 202827
5
ANDROMEDA COMPUTER AND SOFTWARE
ACADEMY
Output:
Task: Write a program that counts how many even numbers are in an array of 6 integers.
Hint: Use the modulus operator % to check if a number is even.
#include <iostream>
using namespace std;
int main() {
int numbers[6] = {4, 7, 10, 3, 8, 5};
int evenCount = 0;
cout << "Number of even numbers: " << evenCount << endl;
return 0;
}
Output:
Main Office:-Gondar Behind PLaza Hotel Tel:-+251 918 707117/+251 953 202827
6
ANDROMEDA COMPUTER AND SOFTWARE
ACADEMY
Part 3: Structures
#include <iostream>
using namespace std;
struct Student {
string name;
int age;
double grade;
};
int main() {
Student student1;
student1.name = "Abebe kebede";
student1.age = 16;
student1.grade = 88.5;
Output:
#include <iostream>
using namespace std;
struct Book {
string title;
string author;
double price;
};
int main() {
Main Office:-Gondar Behind PLaza Hotel Tel:-+251 918 707117/+251 953 202827
7
ANDROMEDA COMPUTER AND SOFTWARE
ACADEMY
Book books[3];
Output:
Task: Define a structure Car with make, model, and year. Create an instance of Car for a
car owned by 'Abebe' and display the information.
Hint: Use the struct keyword to define a structure.
#include <iostream>
using namespace std;
struct Car {
string make;
string model;
int year;
};
int main() {
Car abebesCar;
abebesCar.make = "Toyota";
abebesCar.model = "Corolla";
abebesCar.year = 2018;
Output:
Main Office:-Gondar Behind PLaza Hotel Tel:-+251 918 707117/+251 953 202827
8
ANDROMEDA COMPUTER AND SOFTWARE
ACADEMY
Car Make: Toyota
Car Model: Corolla
Year: 2018
Task: Define a structure Employee with name, id, and salary. Create an instance of
Employee for 'Alemu' and display the information.
Hint: Use the struct keyword to define a structure.
#include <iostream>
using namespace std;
struct Employee {
string name;
int id;
double salary;
};
int main() {
Employee alemu;
alemu.name = "Alemu";
alemu.id = 101;
alemu.salary = 2500.50;
Output:
Main Office:-Gondar Behind PLaza Hotel Tel:-+251 918 707117/+251 953 202827
9
ANDROMEDA COMPUTER AND SOFTWARE
ACADEMY
Part 4: Combined Topics
#include <iostream>
using namespace std;
struct Day {
string dayName;
double temperature;
};
int main() {
Day week[7] = {
{"Monday", 22.5},
{"Tuesday", 24.0},
{"Wednesday", 23.0},
{"Thursday", 25.0},
{"Friday", 21.5},
{"Saturday", 26.0},
{"Sunday", 24.5}
};
Output:
Main Office:-Gondar Behind PLaza Hotel Tel:-+251 918 707117/+251 953 202827
1
ANDROMEDA COMPUTER AND SOFTWARE 0
ACADEMY
2. Problem: Track and Display Students’ Grades
o Task: Define a structure Student with name, age, and an array
of 3 grades. Create an array of 2 students, calculate the
average grade for each student using a function, and display
the results.
#include <iostream>
using namespace std;
struct Student {
string name;
int age;
double grades[3];
};
int main() {
Student students[2] = {
{"Abebe", 17, {85.0, 90.0, 88.0}},
{"Kebede", 16, {78.0, 82.0, 80.0}}
};
Output:
Main Office:-Gondar Behind PLaza Hotel Tel:-+251 918 707117/+251 953 202827
1
ANDROMEDA COMPUTER AND SOFTWARE 1
ACADEMY
3. Problem: Track Inventory for a Bookstore
o Task: Define a structure Book with title, author, price, and
quantity. Create an array of 3 books in a bookstore and write
a function to calculate the total value of the inventory.
#include <iostream>
using namespace std;
struct Book {
string title;
string author;
double price;
int quantity;
};
int main() {
Book bookstore[3] = {
{"The Alchemist", "Paulo Coelho", 15.99, 5},
{"1984", "George Orwell", 9.99, 8},
{"To Kill a Mockingbird", "Harper Lee", 12.99, 3}
};
cout << "Total Inventory Value: $" << totalValue << endl;
return 0;
}
Output: