0% found this document useful (0 votes)
8 views15 pages

Array Code Module 5

The document contains programming questions and solutions in C, covering topics such as calculating the mean of an array, string concatenation, storing and displaying temperatures in a 2D array, string length calculation, string comparison, palindrome checking, counting characters in a string, and matrix operations. Each question is accompanied by step-by-step logic and example outputs to illustrate the solutions. The document serves as a guide for practicing fundamental programming concepts in C.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views15 pages

Array Code Module 5

The document contains programming questions and solutions in C, covering topics such as calculating the mean of an array, string concatenation, storing and displaying temperatures in a 2D array, string length calculation, string comparison, palindrome checking, counting characters in a string, and matrix operations. Each question is accompanied by step-by-step logic and example outputs to illustrate the solutions. The document serves as a guide for practicing fundamental programming concepts in C.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PROGRAMMING QUESTIONS

1. Write a program to calculate the mean of an array of integers.

Iteration (i) Input Value Action Performed Running Sum

i=0 10 sum = 0 + 10 10

i=1 20 sum = 10 + 20 30

i=2 30 sum = 30 + 30 60

i=3 40 sum = 60 + 40 100

i=4 50 sum = 100 + 50 150


2. Write a C program to concatenate two strings without using inbuilt
function.

Step-by-Step Logic
Let’s assume:

 str1 = "Hello"
 str2 = "World"

Step 1: Find end of str1

str1 = H e l l o \0
Indexes: 0 1 2 3 4 5
i = 0 → str1[i] = 'H'
i = 1 → str1[i] = 'e'
i = 2 → str1[i] = 'l'
i = 3 → str1[i] = 'l'
i = 4 → str1[i] = 'o'
i = 5 → str1[i] = '\0' → STOP

Now i = 5, which is the index after 'o'.

Step 2: Copy str2 into str1

str2 = W o r l d \0
Indexes: 0 1 2 3 4 5

j str2[j] Assigned str1[i] New New j


to i
0 W str1[5] = W W 6 1
1 o str1[6] = o o 7 2
2 r str1[7] = r r 8 3
3 l str1[8] = l l 9 4
4 d str1[9] = d d 10 5
5 '\0' STOP

Finally add:

str1[10] = '\0'

Final Result
HelloWorld

Sample Output
Enter first string: Hello
Enter second string: World
Concatenated String: HelloWorld

6. Write a program to concatenate two strings using library functions.


3. Write a C program to store temperature of two cities of a week and
display it using 2D array.
How It Works
2D array declaration
temp[2][7]

 2 rows → City 1, City 2


 7 columns → Day 1 to Day 7

Example Input
Enter temperatures for City 1:
Day 1: 30
Day 2: 31
Day 3: 29
Day 4: 32
Day 5: 30
Day 6: 28
Day 7: 27

Enter temperatures for City 2:


Day 1: 25
Day 2: 26
Day 3: 27
Day 4: 28
Day 5: 29
Day 6: 30
Day 7: 31

Example Output
--- Temperature of Two Cities (Week Data) ---
City 1: 30 31 29 32 30 28 27
City 2: 25 26 27 28 29 30 31

4. Write a program to calculate the length of a string without using in-built


functions.
How It Works
Step-by-step logic

 A string ends with a null character '\0'.


 We keep checking characters one-by-one.
 As long as the character is NOT '\0', we increase the counter.

Example:

Input:

Hello

Iteration:

i str[i length
]
0 H 1
1 e 2
2 l 3
3 l 4
4 o 5
5 \0 stop
Output:

Length of the string = 5

4. Write a program to compare two strings using library functions.

How strcmp() Works


strcmp(str1, str2) returns:

 0 → both strings are equal


 > 0 → str1 is greater (alphabetically comes later)
 < 0 → str1 is smaller

Example Output
Enter first string: apple
Enter second string: banana
String 1 is smaller than String 2.

7. Write a program to check if the given string is palindrome or not.

Palindrome Example: "madam"


Initial values
 start = 0
 end = 4 (because length = 5)

Iteration Table
Step start str[start end str[end Comparison Result Updated Updated
] ] start end
1 0 m 4 m m == m Match 1 3
2 1 a 3 a a == a Match 2 2
3 stop — — — — Loop — —
(start ends
>=
end)

Final Result

✔ All characters matched → Palindrome

The string is a palindrome.

8. Write a C program to count number of vowels, digits, spaces, consonants &


alphabets in a given string.
What is fgets()?
fgets() is a C library function used to read a string, including spaces, from an input stream
(like keyboard or file).

It is safer than gets() and more powerful than scanf("%s").

Syntax
fgets(destination_string, max_size, input_stream);

Meaning of Parameters:

Parameter Meaning

destination_string The array where the input will be stored

max_size Maximum characters to read (including \0)

input_stream Where to read from (example: stdin for keyboard input)


9. Write a program in C to calculate mean, median, and mode of any given
array.
10. Write code to perform transpose of a matrix without using another
matrix.
11. Write a program to multiply two matrices (2D arrays) in C.

You might also like