0% found this document useful (0 votes)
5 views

c++ worksheet on function,array and structure

The document provides a C++ worksheet focusing on functions, arrays, and structures, with various programming tasks such as calculating the area of a rectangle, converting temperatures, and calculating simple interest. It also covers array operations like finding the maximum value and calculating averages, as well as defining and using structures for storing information about students, books, cars, and employees. Each section includes code examples and expected outputs for clarity.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

c++ worksheet on function,array and structure

The document provides a C++ worksheet focusing on functions, arrays, and structures, with various programming tasks such as calculating the area of a rectangle, converting temperatures, and calculating simple interest. It also covers array operations like finding the maximum value and calculating averages, as well as defining and using structures for storing information about students, books, cars, and employees. Each section includes code examples and expected outputs for clarity.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

1

ANDROMEDA COMPUTER AND SOFTWARE


ACADEMY
C++ Worksheet: Functions, Arrays, and Structures
Part 1: Functions

1. Problem: Calculating the Area of a Rectangle


o Task: Write a function calculateArea that takes the length and
width of a rectangle as parameters and returns the area.
o Hint: Area of a rectangle = length × width.

#include <iostream>
using namespace std;

double calculateArea(double length, double width) {


return length * width;
}

int main() {
double length = 5.0;
double width = 3.0;
cout << "Area of the rectangle: " << calculateArea(length, width) <<
endl;
return 0;
}

Output:

o For a rectangle with a length of 5.0 and width of 3.0, the


area is 15.0.
2. Problem: Convert Temperature from Celsius to Fahrenheit
o Task: Write a function celsiusToFahrenheit that converts a
temperature from Celsius to Fahrenheit.
o Hint: F=9/5×C+32

#include <iostream>
using namespace std;

double celsiusToFahrenheit(double celsius) {


return (celsius * 9.0/5.0) + 32;
}

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:

o 25.0°C is equal to 77.0°F.

3. Problem: Calculate the Simple Interest

 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;

double calculateInterest(double principal, double rate, int time) {


return (principal * rate * time) / 100;
}

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.

4. Problem: Calculate the Total Bill with Discount

 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;

double calculateBill(double price, double discount) {


return price - (price * discount / 100);
}

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

1. Problem: Calculate the Average of 5 Grades


o Task: Write a program that stores 5 grades in an array and
calculates the average.
o Hint: Sum all elements and divide by the number of elements.

#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:

o The average grade is 86.8.

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:

o The maximum value in the array is 9.


3. Problem: Storing and Displaying the Prices of 5 Items

 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;

for(int i = 0; i < 5; i++) {


cout << "Price of item " << i+1 << ": $" << prices[i] << endl;
total += prices[i];
}

cout << "Total Price: $" << total << endl;


return 0;
}

Main Office:-Gondar Behind PLaza Hotel Tel:-+251 918 707117/+251 953 202827
5
ANDROMEDA COMPUTER AND SOFTWARE
ACADEMY
Output:

 Price of item 1: $19.99


 Price of item 2: $29.99
 Price of item 3: $15.99
 Price of item 4: $9.99
 Price of item 5: $49.99
 Total Price: $125.95

4. Problem: Count the Number of Even Numbers in an Array

 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;

for(int i = 0; i < 6; i++) {


if(numbers[i] % 2 == 0) {
evenCount++;
}
}

cout << "Number of even numbers: " << evenCount << endl;
return 0;
}

Output:

 The number of even numbers in the array is 3.

Main Office:-Gondar Behind PLaza Hotel Tel:-+251 918 707117/+251 953 202827
6
ANDROMEDA COMPUTER AND SOFTWARE
ACADEMY
Part 3: Structures

1. Problem: Storing and Displaying Student Information


o Task: Define a structure Student with name, age, and grade.
Create an instance and display the information.
o Hint: Use the struct keyword to define a structure.

#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;

cout << "Student Name: " << student1.name << endl;


cout << "Age: " << student1.age << endl;
cout << "Grade: " << student1.grade << endl;
return 0;
}

Output:

o Student Name: Abebe kebede


o Age: 16
o Grade: 88.5
2. Problem: Storing and Displaying Information of 3 Books
o Task: Define a structure Book with title, author, and price.
Create an array of 3 books and display the information.
o Hint: Use an array of structures.

#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];

books[0] = {"The Great Gatsby", "F. Scott Fitzgerald", 10.99};


books[1] = {"1984", "George Orwell", 8.99};
books[2] = {"To Kill a Mockingbird", "Harper Lee", 12.99};

for(int i = 0; i < 3; i++) {


cout << "Book " << i+1 << " Title: " << books[i].title << endl;
cout << "Author: " << books[i].author << endl;
cout << "Price: $" << books[i].price << endl;
cout << endl;
}
return 0;
}

Output:

o Book 1 Title: The Great Gatsby, Author: F. Scott Fitzgerald,


Price: $10.99
o Book 2 Title: 1984, Author: George Orwell, Price: $8.99
o Book 3 Title: To Kill a Mockingbird, Author: Harper Lee,
Price: $12.99
3. Problem: Storing and Displaying Car Information

 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;

cout << "Car Make: " << abebesCar.make << endl;


cout << "Car Model: " << abebesCar.model << endl;
cout << "Year: " << abebesCar.year << endl;
return 0;
}

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

4. Problem: Storing and Displaying Employee Details

 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;

cout << "Employee Name: " << alemu.name << endl;


cout << "ID: " << alemu.id << endl;
cout << "Salary: $" << alemu.salary << endl;
return 0;
}

Output:

 Employee Name: Alemu


 ID: 101
 Salary: $2500.50

Main Office:-Gondar Behind PLaza Hotel Tel:-+251 918 707117/+251 953 202827
9
ANDROMEDA COMPUTER AND SOFTWARE
ACADEMY
Part 4: Combined Topics

1. Problem: Store and Calculate Average Temperature of a Week


o Task: Define a structure Day with a dayName and temperature.
Create an array of 7 Day structures, one for each day of the
week, and write a function to calculate the average
temperature of the week.

#include <iostream>
using namespace std;

struct Day {
string dayName;
double temperature;
};

double calculateAverageTemperature(Day week[], int size) {


double sum = 0.0;
for(int i = 0; i < size; i++) {
sum += week[i].temperature;
}
return sum / size;
}

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}
};

double average = calculateAverageTemperature(week, 7);


cout << "Average temperature of the week: " << average << "°C" <<
endl;
return 0;
}

Output:

o The average temperature of the week is 23.64°C.

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];
};

double calculateAverageGrade(double grades[], int size) {


double sum = 0.0;
for(int i = 0; i < size; i++) {
sum += grades[i];
}
return sum / size;
}

int main() {
Student students[2] = {
{"Abebe", 17, {85.0, 90.0, 88.0}},
{"Kebede", 16, {78.0, 82.0, 80.0}}
};

for(int i = 0; i < 2; i++) {


double average = calculateAverageGrade(students[i].grades, 3);
cout << "Student: " << students[i].name << endl;
cout << "Average Grade: " << average << endl;
cout << endl;
}
return 0;
}

Output:

o Student: Abebe, Average Grade: 87.67


o Student: Kebede, Average Grade: 80.0

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;
};

double calculateInventoryValue(Book books[], int size) {


double totalValue = 0.0;
for(int i = 0; i < size; i++) {
totalValue += books[i].price * books[i].quantity;
}
return totalValue;
}

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}
};

double totalValue = calculateInventoryValue(bookstore, 3);

for(int i = 0; i < 3; i++) {


cout << "Book: " << bookstore[i].title << endl;
cout << "Author: " << bookstore[i].author << endl;
cout << "Price: $" << bookstore[i].price << endl;
cout << "Quantity: " << bookstore[i].quantity << endl;
cout << endl;
}

cout << "Total Inventory Value: $" << totalValue << endl;

return 0;
}
Output:

Book: The Alchemist, Author: Paulo Coelho, Price: $15.99,


Quantity: 5
 Book: 1984, Author: George Orwell, Price: $9.99, Quantity: 8
 Book: To Kill a Mockingbird, Author: Harper Lee, Price: $12.99,
Quantity: 3
 Total Inventory Value: $209.78
Main Office:-Gondar Behind PLaza Hotel Tel:-+251 918 707117/+251 953 202827

You might also like