You are on page 1of 10

Assignment 2 – CEN Group A Edvin Mehaj

Exercise 1 – Pattern
#include <stdio.h>
#include <stdlib.h>

int main()
{

int n;

printf("Enter the number of rows: ");

scanf("%d",&n);
char a=65;

int reminder=n%27;
int r=n/27;
a+=(reminder+r);

for (int j=0; j<=n; j++)


{

for (int k=1; k<=n; k++)


{
if (a<65)
a=90;
if (k<= n-j)
{
printf(" ");
}

else
printf("%c", a);
}

printf("\n");

a-=1;

return 0;
}

Tracing supposing user gives 4 as input :


n a = a +(n % 27)+(n/27) j j < 4(n) k k <= 4(n) k <= n-j Printed
4 68 0  1   Space
4 68  2   Space
4 68  3   Space
4 68  4  - D
4 68 - \n
4 67 j+1=1  1   Space
4 67  2   Space
4 67  3  - C
4 67  4  - C
4 67 - \n
4 66 j+1=2  1   Space
4 66  2  - B
4 66  3  - B
4 66  4  - B
4 66 - \n
4 65 j+1=3  1  - A
4 65  2  - A
4 65  3  - A
4 65  4  - A
4 65 j+1=4 - - - - TERMINAT
E

Exercise 2 – Class Average


#include <stdio.h>

// Function to calculate the class average based on correct and student


answers //
float calculate_grade(char correct_answers[], char student_answers[][40], int
n, int m) {

// Calculate points per question //


float points = (float)100.0 / m;
float classTotal = 0.0;

// Iterate over each student //


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

// Use a variable to keep track of correct answers //


int correct_count = 0;

// Iterate over each question for the current student //


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

// Check if the student's answer matches the correct answer


if (correct_answers[j] == student_answers[i][j]) {
correct_count++;
}
}

// Calculate the grade for the current student //


float grade = (float)correct_count * points;
classTotal += grade;
}

// Calculate the class average


float class_average = classTotal / n;
return class_average;
}

int main() {
int nrStudents, questions;

// Ask the user to enter the number of students and questions //


printf("Enter the number of students and the number of questions please:
");
scanf("%d %d", &nrStudents, &questions);

char correct_answers[40];

// Ask the user to enter the correct answers //


printf("Enter the correct answers: ");
for (int i = 0; i < questions; i++) {
scanf(" %c", &correct_answers[i]);
}

char student_answers[100][40];
// Ask the user to enter the students' answers //
printf("Enter the students answers: ");
for (int i = 0; i < nrStudents; i++) {
for (int j = 0; j < questions; j++) {
scanf(" %c", &student_answers[i][j]);
}
}

// Calculate and print the class average //


float class_average = calculate_grade(correct_answers, student_answers,
nrStudents, questions);
printf("Class Average: %.2f\n", class_average);

return 0;
}

tracing for input 3( nr of students) and 5 ( questions )


i=0
Memory Correct Student 1 Index C.A == S.A Cnt=0 Points+=cnt(100/5) Class
location answers(C.A) answers(S.A) (i) If C.A==S.A total
Cnt++
101xx A 201yy A 0 C.A(0) == S.A(0) 1 Points = 20 100

102xx B 202yy B 1 C.A(1) == S.A(1) 2 Points = 40

103xx C 203yy C 2 C.A(2) == S.A(2) 3 Points = 60

104xx D 204yy D 3 C.A(3) == S.A(3) 4 Points = 80

105xx E 205yy E 4 C.A(4) == S.A(4) 5 Points = 100

i=1
Memory Correct Student 2 Index C.A == S.A Cnt=0 Points+=cnt(100/5) Class
location answers(C.A) answers(S.A) (i) If C.A==S.A total
Cnt++
101xx A 201yy A 0 C.A(0) == S.A(0) 1 Points = 20 180

102xx B 202yy B 1 C.A(1) == S.A(1) 2 Points = 40

103xx C 203yy C 2 C.A(2) == S.A(2) 3 Points = 60

104xx D 204yy A 3 C.A(3) == S.A(3) 3 Points = 60

105xx E 205yy E 4 C.A(4) == S.A(4) 3 Points = 80

i=2
Memory Correct Student 3 Index C.A == S.A Cnt=0 Points+=cnt(100/5) Class
location answers(C.A) answers(S.A) (i) If C.A==S.A total
Cnt++
101xx A 201yy A 0 C.A(0) == S.A(0) 1 Points = 20 260

102xx B 202yy C 1 C.A(1) == S.A(1) 1 Points = 20

103xx C 203yy C 2 C.A(2) == S.A(2) 1 Points = 40

104xx D 204yy D 3 C.A(3) == S.A(3) 2 Points = 60

105xx E 205yy E 4 C.A(4) == S.A(4) 3 Points = 80

class_average=class_Total/nrOfStudents = 260/3 = 86.67

Exercise 3 – Search in a list (USE AT LEAST 1 FUNCTION)


#include <stdio.h>

// the function is going to compare each of the integers in the array //


void arrCmp(int array[],int numberOfnr, int number) {

// the counter will be used to as a true/false //


int cnt=0;

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


// Compare each of the numbers in list with the number
requested //
if (array[i] == number) {

//If the condition is satisfied the counter will increment //


cnt++;

// If the condition is not satisfied counter will be the same //


else cnt = cnt;
}

if (cnt != 0){

// If the counter has been incremented then the number is in the


list //
printf("Number %d is in the list.", number);
}
// If the counter is still zero then it means the number is not in the
list //
else printf("Number %d is not in the list.", number);

return;
}

int main() {

int nrOfnumbers;

// Ask and scan how many numbers the user is going to store in the list //
printf("Enter the number of numbers you want to store: ");
scanf("%d", &nrOfnumbers);
int list[nrOfnumbers];
int numbers ;

//Ask and scan the numbers in the list //


printf("Enter the numbers: ");
for (int i = 0; i < nrOfnumbers; ++i) {

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

// Ask the user which is the number they are searching for //
printf("Enter the number you are searching for: ");
scanf("%d", &numbers);
// Use the function to compare the numbers stored in array //
arrCmp(list,nrOfnumbers,numbers);

return 0;
}

Exercise 4 - Teams (USE AT LEAST 2 FUNCTIONS)


#include <stdio.h>
void readTeamNames(int nrOfTeams, int pointsNeeded);
void readTeamPoints(int array[]);
int calculatePoints(int points[]);

void readTeamNames(int nrOfTeams, int pointsNeeded) {


char teamNames[100][16];
int points[150];
int sum[nrOfTeams];

/* declaire a variable to track if any team satisfies the condition so


* that "0 Teams doesn't print every time the condition is not
* satisfied
*/
int atLeastOneTeamSatisfied = 0;

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

// read team names //


printf("Enter the team name : \n");
scanf("%s", teamNames[i]);

// call the function that reads results (0, 1, 2) //


readTeamPoints(points);

// popullate sum array by calling the function which converts the


results into points //
sum[i] = calculatePoints(points);
}

// Check if at least one team satisfies the condition


for (int i = 0; i < nrOfTeams; ++i) {
if (sum[i] < pointsNeeded) {
printf("%s %d \n", teamNames[i], sum[i]);
atLeastOneTeamSatisfied = 1; // Set the flag to true //
}
}

// Print "0 Teams" only if no team satisfies the condition


if (!atLeastOneTeamSatisfied) {
printf("0 Teams\n");
}
}
// read team points ( results ) function //
void readTeamPoints(int array[]) {
int num, index = 0;

printf("Enter the team results (enter ';' to stop):\n");

// the condition to stop scanning results when " ; " is given //


while (1) {
if (scanf("%d", &num) == 1) {
// Read a number successfully //
array[index] = num;
index++;

// Check if the array is full //


if (index >= 150) {
printf("Array is full. Exiting...\n");
break;
}
} else {
// Input is not a number //
char ch;
scanf("%c", &ch);

// Check if the entered character is ';'


if (ch == ';') {
break;
}
}
}
}

// function which converts results into points //


int calculatePoints(int points[]) {
int sum = 0;

// convert results //
for (int i = 0; i < 150; ++i) {
if (points[i] == 0)
sum += 0;
else if (points[i] == 1)
sum += 1;
else if (points[i] == 2)
sum += 3;
}

return sum;
}

int main() {
int nrOfTeams, pointsToStay;

printf("Enter the number of teams and points required to stay in league


A : ");
scanf("%d%d", &nrOfTeams, &pointsToStay);

// Declare a char variable which can contain = to the number of teams


char names[20][nrOfTeams];

/* call the function which reads team names firstly


* then it reads each team points
* after reading results it converts them into points and calculates sum
* after calculating sum it checks if any team falls from league A
* and prints the team name and its corresponding sum of points */
readTeamNames(nrOfTeams, pointsToStay);

return 0;
}
After declairing variables and team names arrays, we scan the number of teams
and the number of points required to stay in league A.
Then call the function to read team names and calculate points

Function: readTeamNames
 Iterating over each team:
Team 1:
 Read team name: "Team1"
 Call readTeamPoints function to read results: 0 1 2 ;
 Calculate points for Team1: (0 + 1 + 3) = 4
Team 2:
 Read team name: "Team2"
 Call readTeamPoints function to read results: 2 1 0 ;
 Calculate points for Team2: (3 + 1 + 0) = 4
Team 3:
 Read team name: "Team3"
 Call readTeamPoints function to read results: 1 2 2 ;
 Calculate points for Team3: (1 + 3 + 3) = 7

 Iterate over each team to check if they fall from league A


 Print team name and points only if the team falls
Output:
Team1 4
Team2 4

The third team (Team3) does not fall, so it is not printed

You might also like