0% found this document useful (0 votes)
6 views19 pages

C Program Module 3

The document contains a model question paper focused on programming concepts in C, including definitions and examples of strings and arrays, along with programs for string manipulation and matrix multiplication. It covers one-dimensional and two-dimensional arrays, their declarations, initializations, and important functions for string handling. Additionally, it includes programming exercises for calculating the length of a string, comparing strings, and performing matrix operations.

Uploaded by

yashrajan286
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)
6 views19 pages

C Program Module 3

The document contains a model question paper focused on programming concepts in C, including definitions and examples of strings and arrays, along with programs for string manipulation and matrix multiplication. It covers one-dimensional and two-dimensional arrays, their declarations, initializations, and important functions for string handling. Additionally, it includes programming exercises for calculating the length of a string, comparing strings, and performing matrix operations.

Uploaded by

yashrajan286
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

Model Question Paper- I

Module-3
Q. 05 a Define string. Develop a program to read a string, reverse the string and 6 L3 CO3
print.
Definition of String
In C language, a string is a sequence of characters stored in a character
array and terminated by a null character '\0'.
C does not have a separate string data type.
Strings are declared as:
Syntax
char string_name[size];

Example:
char name[20];

Here, the string can store characters and automatically ends with '\0' (null
character), which marks the end of the string.
Program to Read a String, Reverse It and Print

#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char string[100];
int i;
clrscr();
printf("Enter a string: ");
scanf("%s", string);
int len = strlen(string);
printf("Reversed string: ");
for(i = len - 1; i >= 0; i--)
{
printf("%c", string[i]);
}
getch();
}
Model Question Paper- I

b Define array. List and explain the types of arrays. 6 L2 CO3

Definition of Array

An array is a fixed-size collection of elements of the same data type


stored in continuous memory locations.

In simple words:
An array is a group of similar values stored under one variable
name.
• It can store only same datatype or Homogeneous datatype
• Array size is fix
• Index starts from zero
• Array elements are stored onr after another in memory
• Access using index
Example:

If you want to store marks of 5 students:

Instead of writing:

int m1, m2, m3, m4, m5;

You can write:


int marks[5];

Now:

marks[0]
marks[1]
marks[2]
marks[3]
marks[4]

Each value is called an element, and the number inside [ ] is called the
index (subscript).
Model Question Paper- I

Types of Arrays

1. One-Dimensional Array
2. Two-Dimensional Array
3. Multidimensional Array
One-Dimensional Array (1D Array)

A one-dimensional array uses only one index.

It stores elements in a single row (like a list).

Syntax:

datatype array_name[size];

Example:

int number[5];

Memory looks like:

Index 0 1 2 3 4

Assigning values:

number[0] = 35;
number[1] = 40;
number[2] = 20;
number[3] = 57;
number[4] = 19;

Real Life Example:

Marks of 5 students
Temperatures of 7 days
Model Question Paper- I
Salary of 10 employees

This is the most common type of array.

Two-Dimensional Array (2D Array)

A two-dimensional array uses two indices.

It stores data in rows and columns (like a table or matrix).

Syntax:
datatype array_name[row_size][column_size];
Example:

int sales[3][4];
3 rows
4 columns

It looks like a table:

Col0 Col1 Col2 Col3

Row0
Row1
Row2

Accessing element:

sales[1][2]

Multidimensional Array

An array with more than two indices is called a multidimensional array.

Example:
Model Question Paper- I
int data[2][3][4];

This means:

2 blocks

3 rows

4 columns

It is like: Multiple tables together.

Real Life Example:

School → Classes → Students

Year → Month → Day

✔ Array elements are stored in continuous memory locations.

✔ Index always starts from 0 in C.

✔ Array size must be declared before using (in normal cases).

✔ All elements must be of the same data type.

✔ C does not automatically check array limits (very important).

c Develop a program to multiply two N*N matrices. 8 L3 CO3

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

void main()
{
int m, n, p, q, i, j, k;
int a[10][10], b[10][10], c[10][10];
Model Question Paper- I

clrscr();

// Input size of matrices


printf("Enter the size of matrix A\n");
scanf("%d%d", &m, &n);

printf("Enter the size of matrix B\n");


scanf("%d%d", &p, &q);

// Input elements of matrix A


printf("Enter all the elements of matrix A:\n");
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
scanf("%d", &a[i][j]);
}
}

// Input elements of matrix B


printf("Enter all the elements of matrix B:\n");
for(i = 0; i < p; i++)
{
for(j = 0; j < q; j++)
{
scanf("%d", &b[i][j]);
}
}

// Initialize matrix C with 0


for(i = 0; i < m; i++)
{
for(j = 0; j < q; j++)
{
c[i][j] = 0;
Model Question Paper- I
}
}

// Check if multiplication is possible


if(n != p)
{
printf("Matrix multiplication is not possible\n");
exit(0);
}
else
{
// Multiply matrices
for(i = 0; i < m; i++)
{
for(j = 0; j < q; j++)
{
for(k = 0; k < n; k++)
{
c[i][j] += a[i][k] * b[k][j];
}
}
}
}

// Print matrix A
printf("The elements of matrix A are:\n");
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
printf("%d\t", a[i][j]);
}
printf("\n");
}

// Print matrix B
Model Question Paper- I
printf("The elements of matrix B are:\n");
for(i = 0; i < p; i++)
{
for(j = 0; j < q; j++)
{
printf("%d\t", b[i][j]);
}
printf("\n");
}

// Print matrix C (result)


printf("The multiplication of Matrix A and Matrix B is Matrix C\n");
printf("The elements of matrix C are:\n");
for(i = 0; i < m; i++)
{
for(j = 0; j < q; j++)
{
printf("%d\t", c[i][j]);
}
printf("\n");
}

getch();
}
OUTPUT
The elements of matrix A are:
1 2 3
4 5 6
The elements of matrix B are:
7 8
9 10
11 12
The multiplication of Matrix A and Matrix B is Matrix C
The elements of matrix C are:
58 64
139 154
OR
Model Question Paper- I
Q. 06 a Develop a program to find the length of a string without using built in 6 L3 CO3
function.
Without Built-in Function
#include <stdio.h>

void main()
{
char str[100];
int i, count = 0;

printf("Enter the string: ");


scanf("%s", str);

for(i = 0; str[i] != '\0'; i++)


{
count++;
}

printf("String length is %d", count);


}
Extra
1. strlen() – String Length

Using Built-in Function


#include <stdio.h>
#include <string.h>

void main()
{
char str1[20] = "america";
int len;

len = strlen(str1);
printf("String length is %d", len);
}
Output:
String length is 7
Model Question Paper- I

Important String Handling Functions


2. strcmp() – String Compare

Using Built-in Function


#include <stdio.h>
#include <string.h>

void main()
{
char str1[20], str2[20];
int k;

printf("Enter string1: ");


scanf("%s", str1);

printf("Enter string2: ");


scanf("%s", str2);

k = strcmp(str1, str2);

if(k == 0)
printf("Both strings are same");
else
printf("Strings are different");
}
Without Built-in Function
#include <stdio.h>
void main()
{
char str1[20], str2[20];
int i = 0, flag = 0;

printf("Enter string1: ");


scanf("%s", str1);

printf("Enter string2: ");


Model Question Paper- I
scanf("%s", str2);

while(str1[i] != '\0' || str2[i] != '\0')


{
if(str1[i] != str2[i])
{
flag = 1;
break;
}
i++;
}

if(flag == 0)
printf("Strings are same");
else
printf("Strings are different");
}
3. strcpy() – String Copy

Using Built-in Function


#include <stdio.h>
#include <string.h>
void main()
{
char str1[20] = "america";
char str2[20];
strcpy(str2, str1);
printf("Copied string is %s", str2);
}
Without Built-in Function
#include <stdio.h>
void main()
{
char str1[20], str2[20];
int i;
printf("Enter string: ");
scanf("%s", str1);
Model Question Paper- I
for(i = 0; str1[i] != '\0'; i++)
{
str2[i] = str1[i];
}
str2[i] = '\0';

printf("Copied string is %s", str2);


}
4. strcat() – String Concatenation

Using Built-in Function


#include <stdio.h>
#include <string.h>
void main()
{
char str1[20] = "bangalore";
char str2[20] = "city";
strcat(str1, str2);
printf("%s", str1);
}
Output:
bangalorecity
Without Built-in Function
#include <stdio.h>
void main()
{
char str1[20], str2[20], str3[50];
int i, k = 0;
printf("Enter string1: ");
scanf("%s", str1);
printf("Enter string2: ");
scanf("%s", str2);
for(i = 0; str1[i] != '\0'; i++)
{
str3[k++] = str1[i];
}
Model Question Paper- I
for(i = 0; str2[i] != '\0'; i++)
{
str3[k++] = str2[i];
}

str3[k] = '\0';

printf("Concatenated string is %s", str3);


}
5. strrev() – String Reverse
Using Built-in Function
#include <stdio.h>
#include <string.h>
void main()
{
char str1[20] = "imran";

strrev(str1);

printf("%s", str1);
}
Without Built-in Function
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char string[100];
int i;
clrscr();

printf("Enter a string: ");


scanf("%s", string);

int len = strlen(string);


Model Question Paper- I
printf("Reversed string: ");
for(i = len - 1; i >= 0; i--)
{
printf("%c", string[i]);
}

getch();
}
b Explain declaring and initialization one, and two-dimensional arrays 6 L2 CO3
with suitable examples.
One-Dimensional Array (1D Array)

A one-dimensional array stores elements in a single row (like a list).


Declaration of 1D Array

Syntax:

data_type array_name[size];

Example:

int marks[5];

✔ int → data type

✔ marks → array name

✔ 5 → size (can store 5 elements)

✔ Index starts from 0 to 4

Initialization of 1D Array

1. Compile Time Initialization

Syntax:

data_type array_name[size] = {values};


Model Question Paper- I
Example:

int marks[5] = {10, 20, 30, 40, 50};

Memory representation:

Index Value

0 10
1 20
2 30
3 40
4 50

Size Omitted Initialization

int marks[] = {10, 20, 30, 40, 50};

Compiler automatically sets size = 5.

Partial Initialization

int marks[5] = {10, 20};

Remaining elements become 0:

{10, 20, 0, 0, 0}

2. Run Time Initialization

int marks[5];
for(int i = 0; i < 5; i++)
{
scanf("%d", &marks[i]);
}
Model Question Paper- I
Values are entered by the user.

Two-Dimensional Array (2D Array)

A two-dimensional array stores data in rows and columns (like a table


or matrix).
Declaration of 2D Array

Syntax:

data_type array_name[row_size][column_size];

Example:

int table[2][3];

✔ 2 rows

✔ 3 columns

✔ Total elements = 2 × 3 = 6

Index form:

table[0][0] , table[0][1] ,table[0][2]


table[1][0] , table[1][1], table[1][2]

Initialization of 2D Array

1. Compile Time Initialization

int table[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
Model Question Paper- I
Table representation:
1 2 3
4 5 6

Without Inner Braces

int table[2][3] = {1,2,3,4,5,6};

Same output as above (row by row filling).

Partial Initialization

int table[2][3] = {
{1, 2},
{3}
};

Becomes:

1 2 0
3 0 0

Remaining values automatically become 0.

Size Omitted (Row Size)

int table[][3] = {
{1, 2, 3},
{4, 5, 6}
};
Row size automatically becomes 2.
2. Run Time Initialization

int table[2][3];

for(int i = 0; i < 2; i++)


Model Question Paper- I
{
for(int j = 0; j < 3; j++)
{
scanf("%d", &table[i][j]);
}
}

User enters values.

Important Points

✔ Array index always starts from 0

✔ 1D array → one subscript → a[i]


✔ 2D array → two subscripts → a[i][j]

✔ If values are less during initialization → remaining become 0

✔ If values exceed size → compiler error

Remember
Things Not allowedin Arrays

Case Example Why


Too many values Array size = 3, 4 values given →
int marks[3] = {10,20,30,40};
(1D) Error
Too many values Each row has 2 columns, first row
int table[2][2] = {{1,2,3},{4,5}};
(2D) has 3 → Error
Column size must be written →
Omit column size int table[][] = {{1,2,3},{4,5,6}};
Error
c Develop a program to read N numbers and find the sum and average of 8 L3 CO3
N numbers using an array.
#include <stdio.h>
#include <conio.h>

void main()
{
int n, i;
float sum = 0, average;
clrscr();

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


Model Question Paper- I
scanf("%d", &n);

int numbers[n]; // declare array of size n

printf("Enter %d numbers:\n", n);


for(i = 0; i < n; i++)
{
scanf("%d", &numbers[i]);
}

// Calculate the sum


for(i = 0; i < n; i++)
{
sum =sum+ numbers[i];
}

// Calculate the average


average = sum / n;

// Print the results


printf("Sum of numbers = %.2f\n", sum);
printf("Average of numbers = %.2f\n", average);

getch();
}
OUTPUT
Enter the number of elements: 5
Enter 5 numbers:
10 20 30 40 50
Sum of numbers = 150.00
Average of numbers = 30.00

You might also like