Tutorial 11 - Arrays

 1. Find the minimum and maximum of sequence of 10 numbers stored in array.

#include<stdio.h>

int main()

{

float number[10]={25,12,78,95,63.2,75,96,64,58,37};

float min=number[0],max=number[0];

int i;

for(i=1;i<10;i++)

{

if(max<number[i])

{

max=number[i];

}

if(min>number[i])

{

min=number[i];

}

}

printf("Maximum = %.2f,Minimum = %.2f",max,min);

return 0;

}


2. Find the total and average of a sequence of 10 numbers stored in array.

#include<stdio.h>

int main()

{

float num[10],tot,ave;

int c,i;

for(i=0;i<10;i++)

{

printf("Input your number= ");

scanf("%f",&num[i]);

tot=tot+num[i];

}

ave=tot/10;

printf("total=%.2f Average=%.2f",tot,ave);

return 0;

}

4. Following list represents the data units used by three persons. Create an array to store

these details and print the list.

person work mobile  home

1         500     1000     300

2         10       1800     200

3 2       200     400       700

Find the following using the above table;

I. Find the total unit consumed.

II. Find the total consumption of units for each phone type.

III. Who consumed maximum unites for mobile phones?

IV. Who pays highest phone bill?

V. Who pays lower phone bill?

VI. How many units are consumed by person 3?

#include <stdio.h>

int main() {

    // Define and initialize the data units array

    int dataUnits[3][3] = {

        {500, 1000, 300},

        {10, 1800, 200},

        {200, 400, 700}

    };

    // Variables to store results

    int total_units = 0;

    int total_mobile = 0;

    int max_mobile_units = 0, max_mobile_person = 0;

    int max_bill_person = 0, min_bill_person = 0;

    int person3_units = 0;


    // Loop through the data units array to calculate the results

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

        // I. Find the total unit consumed

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

            total_units += dataUnits[i][j];

        }


        // II. Find the total consumption of units for mobile phones

        total_mobile += dataUnits[i][1];


        // III. Who consumed maximum units for mobile phones

        if (dataUnits[i][1] > max_mobile_units) {

            max_mobile_units = dataUnits[i][1];

            max_mobile_person = i + 1;

        }


        // IV. Who pays the highest phone bill

        if (dataUnits[i][1] + dataUnits[i][2] > dataUnits[max_bill_person][1] + dataUnits[max_bill_person][2]) {

            max_bill_person = i + 1;

        }


        // VI. How many units are consumed by person 3

        if (i == 2) {

            person3_units = dataUnits[i][0] + dataUnits[i][1] + dataUnits[i][2];

        }

    }


    // V. Who pays the lower phone bill

    min_bill_person = (max_bill_person == 1) ? 2 : 1;


    // Print the results

    printf("I. Total units consumed: %d\n", total_units);

    printf("II. Total consumption of units for mobile phones: %d\n", total_mobile);

    printf("III. Person %d consumed the maximum units for mobile phones (%d units).\n", max_mobile_person, max_mobile_units);

    printf("IV. Person %d pays the highest phone bill.\n", max_bill_person);

    printf("V. Person %d pays the lower phone bill.\n", min_bill_person);

    printf("VI. Person 3 consumed %d units.\n", person3_units);

    return 0;

}

5. Write a program to store students’ marks for COM1407 subject. Assume there are 10
students in the class. You have to store marks in a 2D array as shown below.
Mid term
examination
(20%)
Portfolio
(10%)
End semester
examination
(100%)
Final Marks
(70%)
Total
(mid term
examination
+portfolio
+final marks
Rounded
Marks
Note: Use keyboard input appropriately.
Rajarata University of Sri Lanka
Department of Computing
ICT 1402 - Principles of Program Design and Programming
Tutorial 11 / Portfolio Activity
2
Find the following using the above entered marks;
I. Display detailed mark sheet
II. Find the highest marks for midterm examination, portfolio work and end semester
examination separately
III. Find the student count based on the following criteria
a. Rounded Marks >= 90% : Grade A
b. Rounded Marks >= 80% : Grade B
c. Rounded Marks >= 70% : Grade C
d. Rounded Marks >= 60% : Grade D
e. Rounded Marks >= 40% : Grade E
f. Rounded Marks < 40% : Grade F
Your output should be displayed as follows
Grade Count
A 
B 
C 
D 
E 

#include <stdio.h>

int main() {
    const int num_students = 10;
    int marks[num_students][4];
    int i;
    int highest_midterm, highest_portfolio, highest_end_semester;
    int grade_count[7] = {0}; // Changed size to 7

    printf("Enter marks for %d students:\n", num_students);
    for (i = 0; i < num_students; i++) {
        printf("Enter marks for student %d (Mid term, Portfolio, End semester): ", i + 1);
        scanf("%d %d %d", &marks[i][0], &marks[i][1], &marks[i][2]);
        marks[i][3] = (marks[i][0] * 0.2) + (marks[i][1] * 0.1) + (marks[i][2] * 0.7);
    }

    printf("\nDetailed Mark Sheet:\n");
    printf("Student\tMid term\tPortfolio\tEnd semester\tFinal Marks\tRounded Marks\n");
    for (i = 0; i < num_students; i++) {
        printf("%d\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", i + 1, marks[i][0], marks[i][1], marks[i][2], marks[i][3], marks[i][3]);
    }

    highest_midterm = highest_portfolio = highest_end_semester = marks[0][0]; // Initialize with first student's marks

    for (i = 1; i < num_students; i++) { // Start from index 1
        if (marks[i][0] > highest_midterm) {
            highest_midterm = marks[i][0];
        }
        if (marks[i][1] > highest_portfolio) {
            highest_portfolio = marks[i][1];
        }
        if (marks[i][2] > highest_end_semester) {
            highest_end_semester = marks[i][2];
        }
    }

    printf("\nHighest Marks:\n");
    printf("Mid term: %d\n", highest_midterm);
    printf("Portfolio: %d\n", highest_portfolio);
    printf("End semester: %d\n", highest_end_semester);

    for (i = 0; i < num_students; i++) {
        int rounded_marks = marks[i][3];
        char grade;
        if (rounded_marks >= 90) {
            grade = 'A';
        } else if (rounded_marks >= 80) {
            grade = 'B';
        } else if (rounded_marks >= 70) {
            grade = 'C';
        } else if (rounded_marks >= 60) {
            grade = 'D';
        } else if (rounded_marks >= 40) {
            grade = 'E';
        } else {
            grade = 'F';
        }
        grade_count[grade - 'A']++;
    }

    printf("\nGrade Count:\n");
    printf("A -> %d\n", grade_count[0]);
    printf("B -> %d\n", grade_count[1]);
    printf("C -> %d\n", grade_count[2]);
    printf("D -> %d\n", grade_count[3]);
    printf("E -> %d\n", grade_count[4]);
    printf("F -> %d\n", grade_count[5]);
    printf("U -> %d\n", grade_count[6]); // Added for grade 'F' counts

    return 0;
}

6. Assume a village has 20 houses. Input electricity unit charges and calculate total
electricity bill according to the following criteria:
 For first 50 units Rs. 0.50/unit
 For next 100 units Rs. 0.75/unit
 For next 100 units Rs. 1.20/unit
 For unit above 250 Rs. 1.50/unit
An additional surcharge of 20% is added to the bill
I. Write a program to store house address, units consumed, surcharge and total
amount separately in arrays.
II. Display the detailed monthly consumption report of the village using the
following order
Serial Number    House Address     Units     Surcharge     Amount to be paid
III. Calculate the total earnings of the month
IV. Calculate the total units consumed

#include <stdio.h>


int main() {

    const int num_houses = 20;

    const float surcharge_rate = 0.20;


    int units_consumed[num_houses] = {0};

    float surcharge[num_houses] = {0};

    float amount_to_be_paid[num_houses] = {0};

    int i;

    float total_earnings = 0;

    int total_units_consumed = 0;


    float unit_charges;

    printf("Enter electricity unit charges: ");

    scanf("%f", &unit_charges);


    printf("Enter units consumed for each house:\n");

    for (i = 0; i < num_houses; i++) {

        printf("House %d: ", i + 1);

        scanf("%d", &units_consumed[i]);

    }


    for (i = 0; i < num_houses; i++) {

        if (units_consumed[i] <= 50) {

            surcharge[i] = 0;

            amount_to_be_paid[i] = units_consumed[i] * unit_charges;

        } else if (units_consumed[i] <= 150) {

            surcharge[i] = surcharge_rate * (units_consumed[i] * unit_charges);

            amount_to_be_paid[i] = units_consumed[i] * unit_charges + surcharge[i];

        } else if (units_consumed[i] <= 250) {

            surcharge[i] = surcharge_rate * (units_consumed[i] * unit_charges);

            amount_to_be_paid[i] = units_consumed[i] * unit_charges + surcharge[i];

        } else {

            surcharge[i] = surcharge_rate * (units_consumed[i] * unit_charges);

            amount_to_be_paid[i] = units_consumed[i] * unit_charges + surcharge[i];

        }


        total_earnings += amount_to_be_paid[i];

        total_units_consumed += units_consumed[i];

    }


    printf("\nDetailed Monthly Consumption Report:\n");

    printf("Serial Number\tUnits\tSurcharge\tAmount to be paid\n");

    for (i = 0; i < num_houses; i++) {

        printf("%d\t\t%d\t%.2f\t\t%.2f\n", i + 1, units_consumed[i], surcharge[i], amount_to_be_paid[i]);

    }


    printf("\nTotal Earnings of the Month: %.2f\n", total_earnings);

    printf("Total Units Consumed: %d\n", total_units_consumed);


    return 0;

}

6. Extend the following program that you did in Tutorial 05 to store the processed bills in a
buffer.
Develop a program to a cashier system for a shop.
Your system should display following prompt for the cashier and the entire program is
controlled according to the command given by the cashier.
Once you press S: System will display welcome message and ready for processing a bill
by initializing required variables.
Once you press Q: You can shut down the cashier system.
Once you press P: Process the current customer’s shopping cart. Here, you can enter the
item code, qty, unit price of the items in shopping cart and calculate and display the total
Once you finished serving to the current customer, you can press N to move to the
prompt.


8. Design a program for payroll department to calculate the monthly payment of 10
employees. Use arrays appropriately. Finally you need to display 10 pay sheets one after
the other.
Welcome to <shop name>
Press S  Start
Press Q  Quit
Press P  Process Bill
Your option : _

#include <stdio.h>

typedef struct {
int employee_id;
float hourly_rate;
float hours_worked;
} Employee;

int main() {
char option;
Employee employees[10];
float total_pay;
int i;

do {
printf("Welcome to the Payroll Department\n");
printf("Press S to start, Q to quit, or P to process payroll: ");
scanf(" %c", &option);

switch (option) {
case 'S':
printf("Initializing payroll system.\n");
// Input employee data
for (i = 0; i < 10; i++) {
printf("Enter details for employee %d:\n", i + 1);
printf("Employee ID: ");
scanf("%d", &employees[i].employee_id);
printf("Hourly rate: ");
scanf("%f", &employees[i].hourly_rate);
printf("Hours worked: ");
scanf("%f", &employees[i].hours_worked);
}
break;
case 'P':
printf("Processing payroll...\n");

printf("Pay Sheets:\n");
for (i = 0; i < 10; i++) {
float total_pay = employees[i].hourly_rate * employees[i].hours_worked;
printf("Employee ID: %d, Total Pay: $%.2f\n", employees[i].employee_id, total_pay);
}
break;
case 'Q':
printf("Exiting payroll system. Goodbye!\n");
break;
default:
printf("Invalid option. Please try again.\n");
break;
}
} while (option != 'Q');

return 0;
}


Post a Comment

Previous Post Next Post