0% found this document useful (0 votes)
22 views23 pages

CSE Assignment 02

Uploaded by

Talha Jobair
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
22 views23 pages

CSE Assignment 02

Uploaded by

Talha Jobair
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 23

CSE 101

Assignment 02

Name : Talha Jobair


ID : 2023-1-60-120
Course : Programming Structure
Section : 11

1.
Function to print a custom message.

Sample input Sample output


This is a function

#include <stdio.h>

void printCustomMessage(char* message) {


printf("%s\n", message);
}

int main() {
printCustomMessage("This is a custom message.");
return 0;
}
2.
Function to print an input character value.

Sample input Sample output


3 Value received from main: 3
A Value received from main: A

#include <stdio.h>
void printCharValue(char c) {
printf("Value received from main: %c\n", c);
}
int main() {
char myChar;
printf("Enter any character: ");
scanf("%c",&myChar);
printCharValue(myChar);
return 0;
}
3.
Function to calculate the sum of n numbers coming from the console.

Sample input Sample output


80 33 27 Sum In Function: 140
Sum In Main: 140
100 -100 Sum In Function: 0
Sum In Main: 0

#include <stdio.h>
int sumOfNNumbers(int n) {
int sum = 0, num;
for (int i = 0; i < n; i++) {
printf("Enter number %d: ", i+1);
scanf("%d", &num);
sum += num;
}
return sum;
}
int main() {
int n = 3;
printf("Sum of the %d numbers is %d.\n", n, sumOfNNumbers(n));
return 0;
}
4.
Function to calculate the sum of n numbers coming from the console and stored in an array.

Sample input Sample output


3 Sum In Function: 140
80 33 27 Sum In Main: 140

2 Sum In Function: 0
100 -100 Sum In Main: 0

#include <stdio.h>
int sum(int arr[], int n) {
int i, sum = 0;
for (i = 0; i < n; i++) {
sum += arr[i];
}
printf("Sum In Function: %d\n", sum);
return sum;
}
int main() {
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int totalSum = sum(arr, n);
printf("Sum In Main: %d\n", totalSum);
return 0;
}
5.
Function to swap two numbers.
(Restriction: Pass by value)

Sample input Sample output


10 20 Value in func: 20 10
Value in main: 10 20

#include <stdio.h>
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x,y;
printf("Enter two numbers: ");
scanf("%d %d",&x,&y);
printf("Value in func: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("Value in main: x = %d, y = %d\n", x, y);
return 0;
}
6.
Function to swap two numbers.
(Restriction: Pass by reference)

Sample input Sample output


10 20 Value in func: 20 10
Value in main: 20 10

#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int num1,num2;
printf("Enter two integers : ");
scanf("%d %d",&num1,&num2);
printf("Value in fun: %d %d\n", num2, num1);
swap(&num1, &num2);
printf("Value in main: %d %d\n", num1, num2);

return 0;
}
7.
Function to determine only even numbers in an array of input integers.

Sample input Sample output


24 77 117 -512 1024 24 -512 1024
45 33 0 256 0 256

#include<stdio.h>
void even_numbers(int arr[], int size) {
printf("Even numbers in the array: ");
for(int i = 0; i < size; i++) {
if(arr[i] % 2 == 0) {
printf("%d ", arr[i]);
}
}
}
int main() {
int arr[5];
printf("Enter the numbers in array : ");
scanf("%d %d %d %d %d",&arr[0],&arr[1],&arr[2],&arr[3],&arr[4]);
int size = sizeof(arr) / sizeof(arr[0]);
even_numbers(arr, size);
return 0;
}
8.
Function that finds and returns the minimum value in an array.

Sample input Sample output


157 -28 -37 26 10 Minimum Value: -37
12 45 1 10 5 3 22 Minimum Value: 1

#include <stdio.h>
int findMinimum(int arr[], int n) {
int i;
printf("how many numbers : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
int min = arr[0];
for (i = 1; i < n; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}
int main() {
int arr[100],n;
int min = findMinimum(arr, n);
printf("The minimum value in the array is: %d\n", min);
return 0;
}
9.
Function that multiplies the array elements by 2 and returns the array.

Sample input Sample output


157 -28 -37 26 10 314 -56 -74 52 20
12 45 1 10 5 3 22 24 90 2 20 10 6 44

#include <stdio.h>
int* multiplyByTwo(int arr[], int n) {
int i;
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
static int result[100];
for (i = 0; i < n; i++) {
result[i] = arr[i] * 2;
}
return result;
}
int main() {
int arr[100],n;
printf("How many numbers: ");
scanf("%d",&n);
int* result = multiplyByTwo(arr, n);
printf("The resulting array after multiplying each element by 2: ");
for (int i = 0; i < n; i++) {
printf("%d ", result[i]);
}
printf("\n");
return 0;
}
10.
Function to sort and return an input array in ascending order.

Sample input Sample output


10 22 -5 117 0 -5 0 10 22 117

#include<stdio.h>
int sort_ascending(int arr[], int n) {
int i;
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
for(int i = 0; i < n - 1; i++) {
for(int j = i + 1; j < n; j++) {
if(arr[i] > arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
int main() {
int arr[100],n;
printf("How many numbers: ");
scanf("%d",&n);
int* sorted_arr = sort_ascending(arr, n);
printf("Sorted array in ascending order: ");
for(int i = 0; i < n; i++) {
printf("%d ", sorted_arr[i]);
}
return 0;
}
11.
Function “IsPrime()” to determine whether a number is prime or not.

Sample input Sample output


1 Not prime
2 Prime
11 Prime
39 Not prime
101 Prime

#include<stdio.h>
int isPrime(int n) {
if (n < 2) {
return 0;
}
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
return 0;
}
}
return 1;
}
int main() {
int n;
printf("Enter any number: ");
scanf("%d",&n);
if (isPrime(n)) {
printf("%d is prime\n", n);
} else {
printf("%d is not prime\n", n);
}
return 0;
}
12.
Function “GeneratePrime()” to compute the prime numbers less than N, where N is an input
integer. GeneratePrime() uses IsPrime() to check whether a number is prime or not.

Sample input Sample output


5 Prime less than 5: 2, 3
10 Prime less than 10: 2, 3, 5, 7
40 Prime less than 17: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37

#include<stdio.h>
int isPrime(int n) {
if (n < 2) {
return 0;
}
for (int i = 2; i*i <= n; i++) {
if (n % i == 0) {
return 0;
}
}
return 1;
}
void generatePrime(int n) {
printf("Prime numbers less than %d:\n", n);
for (int i = 2; i < n; i++) {
if (isPrime(i)) {
printf("%d ", i);
}
}
printf("\n");
}
int main() {
int n;
printf("Enter any number: ");
scanf("%d",&n);
generatePrime(n);
return 0;
}
13.
Function “GenNthPrime()” to compute the N th prime number, where N is an integer input.

Sample input Sample output


5 5th Prime: 11
10 10th Prime: 29
40 40th Prime: 173

#include <stdio.h>
int isPrime(int n) {
if (n < 2) {
return 0;
}
for (int i = 2; i*i <= n; i++) {
if (n % i == 0) {
return 0;
}
}
return 1;
}
int genNthPrime(int n) {
int count = 0;
int p = 2;
while (count < n) {
if (isPrime(p)) {
count++;
}
p++;
}
return p-1;
}
int main() {
int n;
printf("Enter any number: ");
scanf("%d",&n);
printf("The %dth prime number is %d\n", n, genNthPrime(n));
return 0;
}
14.
Implement the following functions and calculate standard deviation of an array whose
values come from the terminal-

TakeInput()
CalcMean(array, num_of_elem)
Calc_Std_deviation(array, num_of_elem)

Formula:

Sample input Sample output


45544226 1.32
600 470 170 430 300 147.32
15.
Function find_substr( ) that takes two string arrays (a, b) as parameters, returns 1 if string b
is found anywhere in string a, or returns –1 if no match is found.

(Assuming, strlen(a)&gt;strlen(b))

Sample input (a, b) Sample output


Madam adam 1
Telescope less 0
101010 101 1

#include<stdio.h>
int find_substr(char a[], char b[]) {
int len_a = strlen(a);
int len_b = strlen(b);
for(int i=0; i<=(len_a-len_b); i++){
int j;
for(j=0; j<len_b; j++){
if(a[i+j] != b[j]){
break;
}
}
if(j == len_b){
return 1;
}
}
return -1;
}
int main() {
char a[] = "telescope";
char b[] = "less";
int result = find_substr(a, b);
if(result == 1) {
printf("1 \n");
} else {
printf("0 \n");
}
return 0;
}
16.
Function find_substr( ) that takes two string arrays (a, b) as parameters, uses function
str_length() to determine the lengths of the strings, and then looks for the smaller string
anywhere in the bigger string. It returns 1 if the substring is found, or returns –1 if no match
is found.
[Restriction: str_length() cannot uses built-in strlen() function]

Sample input (a, b) Sample output


madam adam 1
telescope less 0
101010 101 1

#include <stdio.h>
int str_length(char str[]) {
int len = 0;
while (str[len] != '\0') {
len++;
}
return len;
}
int find_substr(char a[], char b[]) {
int len_a = str_length(a);
int len_b = str_length(b);
int i, j;
for (i = 0; i <= len_a - len_b; i++) {
for (j = 0; j < len_b; j++) {
if (a[i+j] != b[j]) {
break;
}
}
if (j == len_b) {
return 1;
}
}
return -1;
}
int main() {
char a[] = "madam";
char b[] = "adam";
if (find_substr(a, b) == 1) {
printf("1 \n");
} else {
printf("0 \n");
}
return 0;
}
17.
Program that continuously takes two positive integers as inputs and uses two functions to
find their GCD (greatest common divisor) and LCM (least common multiple). Both functions
take parameters and returns desired values.
[Hint: Use infinite loop to process inputs]

Sample input Sample output

57 GCD: 1
LCM: 35
12 12 GCD: 12
LCM: 12
12 32 GCD: 4
LCM: 96

#include <stdio.h>
int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
int main() {
int num1, num2;
while (1) {
printf("Enter two positive integers: ");
scanf("%d %d", &num1, &num2);
if (num1 <= 0 || num2 <= 0) {
printf("Invalid input. Both numbers must be positive.\n");
continue;
}
printf("GCD(%d, %d) = %d\n", num1, num2, gcd(num1, num2));
printf("LCM(%d, %d) = %d\n", num1, num2, lcm(num1, num2));
}
return 0;
}
18.
Program that implements function to perform operations on a 3X5 matrix:

InputMatrix()
ShowMatrix()
ScalarMultiply()

#include <stdio.h>
void InputMatrix(int matrix[3][5]) {
int i, j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 5; j++) {
printf("Enter element [%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
}
}
}
void ShowMatrix(int matrix[3][5]) {
int i, j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 5; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
void ScalarMultiply(int matrix[3][5], int scalar) {
int i, j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 5; j++) {
matrix[i][j] *= scalar;
}
}
}
int main() {
int matrix[3][5];
int scalar;
InputMatrix(matrix);
printf("The original matrix:\n");
ShowMatrix(matrix);
printf("Enter a scalar to multiply the matrix by: ");
scanf("%d", &scalar);
ScalarMultiply(matrix, scalar);
printf("The scalar multiplied matrix:\n");
ShowMatrix(matrix);
return 0;
}
19.
Program that implements function to perform operations on a MXN matrix:

InputMatrix()
ShowMatrix()
ScalarMultiply()

#include <stdio.h>
void InputMatrix(int m, int n, int matrix[m][n]) {
int i, j;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("Enter element [%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
}
}
}
void ShowMatrix(int m, int n, int matrix[m][n]) {
int i, j;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
void ScalarMultiply(int m, int n, int matrix[m][n], int scalar) {
int i, j;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
matrix[i][j] *= scalar;
}
}
}
int main() {
int m, n;
printf("Enter number of rows and columns of matrix: ");
scanf("%d %d", &m, &n);
int matrix[m][n];
InputMatrix(m, n, matrix);
printf("The original matrix:\n");
ShowMatrix(m, n, matrix);
int scalar;
printf("Enter a scalar to multiply the matrix by: ");
scanf("%d", &scalar);
ScalarMultiply(m, n, matrix, scalar);
printf("The scalar multiplied matrix:\n");
ShowMatrix(m, n, matrix);
return 0;
}
20.
Program to convert a positive integer to another base using the following functions-
I. Get_Number_And_Base () : Takes number to be converted (N) and base value (B)
from user. Base must be between 2 and 16.
II. Convert_Number () : Does the conversion

III. Show_Converted_Number() : Displays the converted value.

Sample input(N,B) Sample output


100 8 144
512 16 200
512 0 Base not within proper range!

#include <stdio.h>
int num, base, quo, rem, i = 0, j;
char hex[100];
void Get_Number_And_Base () {
printf("Enter the number to be converted: ");
scanf("%d", &num);
printf("Enter the base (between 2 and 16): ");
scanf("%d", &base);
}
void Convert_Number () {
quo = num;
while (quo != 0) {
rem = quo % base;
if (rem < 10) {
hex[i++] = rem + '0';
} else {
hex[i++] = rem + 55;
}
quo = quo / base;
}
}
void Show_Converted_Number() {
printf("The value of %d in base %d is: ", num, base);

for (j = i - 1; j >= 0; j--) {


printf("%c", hex[j]);
}
printf("\n");
}
int main() {
Get_Number_And_Base();
Convert_Number();
Show_Converted_Number();
return 0;
}

You might also like