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

Comp 301 Assignment

Samwel Maturu completed an assignment for his SOEN 301 Data Structures and Algorithms course. The assignment included writing algorithms and programs to: 1) Find the largest number in an array. 2) Find the roots of a quadratic equation. 3) Perform a linear (sequential) search on an array. For each part, Maturu provided pseudocode algorithms and C programs to implement the algorithms.

Uploaded by

dylansamy41
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Comp 301 Assignment

Samwel Maturu completed an assignment for his SOEN 301 Data Structures and Algorithms course. The assignment included writing algorithms and programs to: 1) Find the largest number in an array. 2) Find the roots of a quadratic equation. 3) Perform a linear (sequential) search on an array. For each part, Maturu provided pseudocode algorithms and C programs to implement the algorithms.

Uploaded by

dylansamy41
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

IN16/00001/21

NAME: SAMWEL MONG’ARE MATURU 18/10/2023

REG NO: IN16/00001/21

BACHELOR OF SCIENCE IN SOFTWARE ENGINEERING

SOEN 301: DATA STRUCTURES AND ALLOGORITH ASSIGNMENT

Assignment 1
a) Write an algorithm to find and return the largest of n given numbers in an array
Answer
Algorithm max(myArray):
Step 1: Set largestNumber as the first element of the array (myArray[0]).
Step 2: For i from 1 to n - 1:
a. If myArray[i] is greater than largestNumber:
i. Update largestNumber with myArray [i].
Step 3: Return largestNumber as the result.
b) Write a program using a function max that implements (a)
Answer
#include <stdio.h>
int max(int myArray[], int n) {
int largestNumber = myArray[0];
for (int i = 1; i < n; i++) {
if (myArray[i] > largestNumber) {
largestNumber = myArray[i];
}
}
return largestNumber;
}
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int myArray[size];
printf("Enter %d elements of the array:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &myArray[i]);
}
int largestNumber = max(myArray, size);
printf("The largest number in the array is: %d\n", largestNumber);
return 0;
}
IN16/00001/21

c) Write an algorithm to display the roots of a quadratic equation


Answer
Algorithm FindQuadraticRoots(a, b, c):
Step 1: Read coefficients of a,b and c
Step 2: Calculate discriminant (D) using
D = b^2 - 4ac.
Step 3: If D > 0:
a. Calculate two real roots:
i. root1 = (-b + √D) / (2a)
ii. root2 = (-b - √D) / (2a)
b. Display "Two real roots: root1 and root2".
else if D = 0:
a. Calculate the single real root:
i. root = -b / (2a)
b. Display "One real root: root".
else D < 0:
a. Display "No real roots exist".
[End of if structure]
Step 4: Exit
d) Write a program to implement (c)
Answer
#include <stdio.h>
#include <math.h>
void findQuadraticRoots(double a, double b, double c) {
double discriminant = b * b - 4 * a * c;
double root1, root2;
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Two real roots: %.2f and %.2f\n", root1, root2);
} else if (discriminant == 0) {
root1 = -b / (2 * a);
printf("One real root: %.2f\n", root1);
} else {
printf("No real roots exist.\n");
}
}
int main() {
double a, b, c;
printf("Enter coefficients of the quadratic equation (a, b, c): ");
scanf("%lf %lf %lf", &a, &b, &c);
findQuadraticRoots(a, b, c);
return 0;
}
IN16/00001/21

e) Write a program to implement linear (sequential ) search algorithm


Answer
#include <stdio.h>
int linearSearch(int myArray[], int n, int target) {
int index = -1;
for (int i = 0; i < n; i++) {
if (myArray[i] == target) {
index = i;
break;
}
}
return index;
}
int main() {
int n, target;
printf("Enter the size of the array: ");
scanf("%d", &n);
int myArray[n];
printf("Enter %d elements of the array:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &myArray[i]);
}
printf("Enter the target element to search: ");
scanf("%d", &target);
int index = linearSearch(myArray, n, target);
if (index != -1) {
printf("Target element found at index: %d\n", index);
} else {
printf("Target element not found in the array.\n");
}
return 0;
}

You might also like