0% found this document useful (0 votes)
1 views29 pages

Week1 Code

The document contains multiple programming tasks involving input and output operations, including counting odd and even numbers, calculating scores, matrix operations, and finding maximum and minimum elements in arrays. Each task is accompanied by sample input and output formats, as well as the corresponding C code implementations. The tasks cover various concepts such as array manipulation, matrix multiplication, and data validation.

Uploaded by

againmyjob
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views29 pages

Week1 Code

The document contains multiple programming tasks involving input and output operations, including counting odd and even numbers, calculating scores, matrix operations, and finding maximum and minimum elements in arrays. Each task is accompanied by sample input and output formats, as well as the corresponding C code implementations. The tasks cover various concepts such as array manipulation, matrix multiplication, and data validation.

Uploaded by

againmyjob
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

​ Input
Output
12

23 45 100 32 48 20 10 15 61 27 80 55
Count of Odd elements: 6
Count of Even elements: 6
Input
Output
5

90 15 23 28 75
Count of Odd elements: 3
Count of Even elements: 2

#include <stdio.h>
int main()
{
int n, i, odd_count = 0, even_count = 0;
scanf("%d", &n);
int arr[n];
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < n; i++)
{
if (arr[i] % 2 == 0)
even_count++;
else
odd_count++;
}
printf("Count of Odd elements: %d\n", odd_count);
printf("Count of Even elements: %d", even_count);
return 0;
}

2. inputL
Input

9.5 9.2 8.9 8.8 9.4 7.9 8.8 9.6


Output

Your lowest score is 7.90

Your maximum score is 9.60

Your total point is 54.60

Your average point is 9.10

#include<stdio.h>

int main() {

float arr[8], sum = 0.0, avg, min, max;

int i;

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

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

min = arr[0];

max = arr[0];

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

sum += arr[i];

if(arr[i] < min) {

min = arr[i];
}

if(arr[i] > max) {

max = arr[i];

sum = sum - (min + max);

avg = sum / 6;

printf("Your lowest score is %.2f\n", min);

printf("Your maximum score is %.2f\n", max);

printf("Your total point is %.2f\n", sum);

printf("Your average point is %.2f\n", avg);

return 0;

3. Input

32

7 15

3 9

64
Output
736

15 9 4

Weightage - 10
Input

23

24 59 7

5 0 17
Output

24 5

59 0

7 17
#include <stdio.h>

int main() {

int rows, cols;

scanf("%d", &rows);

scanf("%d", &cols);

int a[rows][cols], b[cols][rows];

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

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

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

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


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

b[j][i] = a[i][j];

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

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

printf("%d ", b[i][j]);

printf("\n");

return 0;

4. Add all the element below the diagonal

Input

100

14 16 0

12 8 12
Output

34
#include <stdio.h>

int main()

int N;

scanf("%d", &N);

int A[N][N];

int row, col, sum = 0;

for(row = 0; row < N; row++)

for(col = 0; col < N; col++)

scanf("%d", &A[row][col]);

for(row = 0; row < N; row++)

for(col = 0; col < N; col++)

if(col < row)

sum += A[row][col];
}

printf("%d\n", sum);

return 0;

1.

Sample Input

36817

6
Sample Output

Elements greater than the element : 8 7

Number of elements lesser are : 2

Sample Input

12 5 27 14 3 19 8

12
Sample Output

Elements greater than the element : 27 14 19

Number of elements lesser are : 3

#include <stdio.h>
int main() {

int N, elementToSearch;

// Read the number of elements

scanf("%d", &N);

// Read the elements of the array

int arr[N];

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

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

// Read the element to be searched

scanf("%d", &elementToSearch);

// Find and print elements greater than the given element

printf("Elements greater than the element : ");

int countLesser = 0;

int first = 1;

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

if (arr[i] > elementToSearch) {

if (!first) {

printf(" ");

printf("%d", arr[i]);

first = 0;

}
if (arr[i] < elementToSearch) {

countLesser++;

printf("\n");

// Print the count of elements lesser than the given


element

printf("Number of elements lesser are : %d\n",


countLesser);

return 0;
}

2 Sarah has a collection of coins, each with a unique value, arranged in ascending
order.

She receives a new coin and wants to determine its position in the collection.
​ If the new coin already exists in the collection, the program should return its
index.
​ If it does not exist, the program should return the index where it should be
inserted to maintain the order.
​ If the collection is not sorted or contains duplicates, the program should
return -1.
Help Sarah in her task.
Input Format
The first line of the input consists of an integer N, representing the size of the array.
The second line of the input consists of N integers, representing the elements of
the array.
The third line of the input consists of an integer X, representing the target value.
Output Format
The output displays an integer representing the index (0-based index) based on the
given conditions.
Refer to the sample output for the formatting specifications.
Constraints
The given test cases fall under the following specifications:
1 ≤ N, X ≤ 100
Sample Input
Sample Output

2468

1
Sample Input
Sample Output

1357

3
Sample Input
Sample Output

2668

-1

#include <stdio.h>

#include <stdbool.h>
int main() {

int n;

scanf("%d", &n);

int arr[n];

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

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

int coin;

scanf("%d", &coin);

bool isSortedUnique = true;

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

if (arr[i] >= arr[i + 1]) {

isSortedUnique = false;

break;

if (!isSortedUnique) {

printf("-1");

return 0;

}
int left = 0, right = n - 1, result = -1;

while (left <= right) {

int mid = (left + right) / 2;

if (arr[mid] == coin) {

result = mid;

break;

} else if (arr[mid] < coin) {

left = mid + 1;

} else {

right = mid - 1;

if (result == -1) {

result = left;

printf("%d", result);

return 0;

3 Problem Statement

Rohan is developing a program for his college project that requires matrix
multiplication. He needs to write a program that takes two 3×3 matrices, A and B,
as input from the user and computes their product to generate matrix C.
Help Rohan complete his program by implementing matrix multiplication and
displaying the resulting matrix C.
Input Format
The input consists of the elements of array A in a 3x3 matrix, followed by a single
line, followed by the elements of array B in a 3x3 matrix.
Output Format
The output displays the product of two matrices.
Refer to the sample output for formatting specifications.
Constraints
The given test cases fall under the following constraints:
3x3 matrix only
Sample Input
Sample Output

123

456

789

123

456

789

30 36 42

66 81 96

102 126 150

Sample Input
Sample Output

11 12 13

14 15 16

17 18 19
11 12 13

14 15 16

17 18 19

510 546 582

636 681 726

762 816 870

Time Limit: - ms Memory Limit: - kb Code Size: - kb

#include<stdio.h>

#define n 3

int main() {

int a[3][3], b[3][3], c[3][3], i, j, k;

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

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

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

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

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

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

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

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

c[i][j] = 0;

for (k = 0; k < n; k++) {

c[i][j] += a[i][k] * b[k][j];

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

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

printf("%d ", c[i][j]);

printf("\n");

return 0;

4. Problem Statement

Dr. Amy, a data scientist, is working on a data transformation task. She needs to
swap two specified columns in a given 2D matrix to prepare her data for analysis.
Help Dr. Amy write a program that swaps the specified columns in the matrix and
prints the result.
Input Format
The first line of input consists of an integer T, representing the number of test
cases.
For each test case:
​ The first line contains two integers R and C, representing the number of rows
and columns in the matrix.
​ The next R lines each contain C space-separated integers, representing the
matrix elements.
​ The last line contains two integers c1 and c2, representing the indices of the
columns to be swapped (0-based).
Output Format
For each test case, print the matrix after swapping the specified columns.
Refer to the sample output for formatting specifications.
Constraints
The given test cases fall under the following constraints:
1 ≤ T ≤ 10
1 ≤ R, C ≤ 5
0 ≤ matrix element ≤ 100
Sample Input

33

123

456

789

02

22

12

34

10
Sample Output

321

654

987
21

43

Sample Input
Sample Output

33

853

416

729

21

835

461

792

Time Limit: - ms Memory Limit: - kb Code Size: - kb

#include <stdio.h>

int main() {

int T;

scanf("%d", &T);

while (T--) {

int R, C, col1, col2;

scanf("%d %d", &R, &C);

int matrix[100][100];
for (int i = 0; i < R; i++) {

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

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

scanf("%d %d", &col1, &col2);

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

int temp = matrix[i][col1];

matrix[i][col1] = matrix[i][col2];

matrix[i][col2] = temp;

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

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

printf("%d ", matrix[i][j]);

printf("\n");

return 0;

1. Problem Statement

Aishu is working on an array manipulation project and needs to find the maximum
and minimum elements in an array of integers.
Write a program that takes an array of integers as input and finds the maximum and
minimum elements in it.
Input Format
The first line consists of an integer n, representing the array size.
The second line input consists of n space-separated integers, representing the
array elements.
Output Format
The first line displays "Maximum element is: " followed by the maximum element of
the array.
The second line displays "Minimum element is: " followed by the minimum element
of the array.
Refer to the sample output for the formatting specifications.
Constraints
The given test cases fall under the following specifications:
2 ≤ n ≤ 20
-1000 ≤ elements ≤ 1000
Sample Input
Sample Output

-34 -57 40 -41 133 77 81

Maximum element is: 133

Minimum element is: -57


Sample Input
Sample Output

-2 0 -7

Maximum element is: 0

Minimum element is: -7


Sample Input
Sample Output
4

99 99 99 99

Maximum element is: 99

Minimum element is: 99


Time Limit: - ms Memory Limit: - kb Code Size: - kb

#include <stdio.h>

int main(){

int arr1[20];

int i, mx, mn, n;

scanf("%d",&n);

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

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

mx = arr1[0];

mn = arr1[0];

for(i=1; i<n; i++){

if(arr1[i]>mx)

mx = arr1[i];

if(arr1[i]<mn)

mn = arr1[i];

}
printf("Maximum element is: %d\n", mx);

printf("Minimum element is: %d", mn);

return 0;

2. Problem Statement

Lucy is studying daily wind speeds for a certain number of days. Given an array
representing these wind speeds, she needs to list all the days where the wind
speeds fall within a specified range (inclusive).
Write a program to help Lucy that should read the number of days, the wind speed
limit, and the wind speeds for each day, and then output the list of all the days
where the wind speeds fall within a specified range.
Input Format
The first line of input consists of an integer T, representing the number of test
cases.
For each test case:
​ The first line contains an integer N, representing the number of days.
​ The second line contains N integers representing the wind speeds for each
day.
​ The third line contains two integers L (lower limit) and U (upper limit) for the
wind speed range.
Output Format
For each test case, print the wind speeds that are within the specified range [L, U].
If no wind speeds fall within the range, print "No days found".
Refer to the sample output for formatting specifications.
Constraints
The given test cases fall under the following constraints:
1 ≤ T ≤ 10
2 ≤ N ≤ 10
0 ≤ wind speed ≤ 100
0 ≤ L < U ≤ 100
L and U are inclusive.
Sample Input

58 100

20 30

93 21 65

1 25
Sample Output

No days found

21

Sample Input

23 56 73 89 29 27 83 72 91

20 40
Sample Output

23 29 27

Time Limit: - ms Memory Limit: - kb Code Size: - kb

#include <stdio.h>
int main() {
int T;
scanf("%d", &T);

while (T--) {
int N, wind[10], L, U;
scanf("%d", &N);

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


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

scanf("%d %d", &L, &U);

int found = 0;

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


if (wind[i] >= L && wind[i] <= U) {
printf("%d ", wind[i]);
found = 1;
}
}

if (!found) {
printf("No days found");
}

printf("\n");
}

return 0;

}
3 Problem Statement
Aryan is an enthusiastic young programmer who loves exploring matrices. He is
currently working on a program to calculate the sum of all even elements in a
square matrix.
He needs your help to complete the code.
Input Format
The first line of input consists of a single integer N, representing the size of the
square matrix.
The next N lines contain N space-separated integers, representing the elements of
the square matrix.
Output Format
If there are even elements in the matrix, display the sum of all even elements.
If there are no even elements in the matrix, display "0".
Refer to the sample output for formatting specifications.
Constraints
In the given scenario, the test cases fall under the following constraints:
1≤N≤8
0 ≤ elements ≤ 100
Sample Input
Sample Output
4
2789
4067
8941

1287

42
Sample Input
Sample Output
2
35

79
0
Sample Input
Sample Output
3
24 57 89
52 69 28

35 76 92

272
Time Limit: - ms Memory Limit: - kb Code Size: - kb

#include <stdio.h>

#define MAX_SIZE 15

int main(){

int arr[MAX_SIZE][MAX_SIZE];

int N;

int i, j;

int sum = 0;

scanf("%d", &N);

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

for (j = 0; j < N; j++){

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

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

for (j = 0; j < N; j++){

if (arr[i][j] % 2 == 0){
sum += arr[i][j];

printf("%d", sum);

return 0;

4. Sita is analyzing data from two different surveys represented as matrices. She
needs to perform Matrix Subtraction, where the second matrix is subtracted
element-wise from the first one. The result should be displayed as a new matrix.

Help her to implement the task.


Input Format
The first line of input consists of two integers R and C, representing the number of
rows and columns of the matrices.
The next R lines consist of C integers each, representing the elements of the first
matrix (matrix1).
The following R lines consist of C integers each, representing the elements of the
second matrix (matrix2).
Output Format
The output prints the resultant matrix after subtraction.
Each row of the resultant matrix is printed in a separate line with space-separated
integers.
Refer to the sample output for formatting specifications.
Constraints
The given test cases fall under the following constraints:
1 ≤ R ≤ 100
1 ≤ C ≤ 100
-1000 ≤ matrix1[i][j], matrix2[i][j] ≤ 1000
Sample Input
Sample Output

22

12

34

56

78

-4 -4

-4 -4

Sample Input
Sample Output

14

10 20 30 40

1234

9 18 27 36

Time Limit: - ms Memory Limit: - kb Code Size: - kb

#include <stdio.h>

int main() {

int R, C;
scanf("%d %d", &R, &C);

int matrix1[R][C], matrix2[R][C], subMatrix[R][C];

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

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

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

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

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

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

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

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

subMatrix[i][j] = matrix1[i][j] - matrix2[i][j];

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

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

printf("%d ", subMatrix[i][j]);

}
printf("\n");

return 0;

You might also like