Assignment no 3
Assignment no 3
piece of code.
Loops in C, like in any programming language, are control flow
structures that allow you to repeat a block of code multiple times.
They're essential for automating repetitive tasks and iterating
over collections of data. There are mainly three types of loops in
C: for, while, and do-while. Let's explain and differentiate them
with examples:
For Loop:
The for loop in C is used when you know the number of iterations
you want to perform.
#include <stdio.h>
int main() {
for (int i = 0; i < 5; i++) {
printf("Iteration %d\n", i);
}
return 0;
}
In this code, the for loop initializes i to 0, continues looping as
long as i is less than 5, and increments i by 1 in each iteration. It
then prints "Iteration" followed by the current value of i.
While Loop:
The while loop in C is used when you want to repeat a block of
code as long as a condition is true.
#include <stdio.h>
int main() {
int count = 0;
while (count < 5) {
printf("Count is %d\n", count);
count++;
}
return 0;
}
Here, the while loop continues to execute as long as count is less
than 5. Inside the loop, it prints the current value of count and
then increments count by 1.
Do-While Loop:
The do-while loop in C is similar to the while loop, but it
guarantees that the block of code is executed at least once before
checking the loop condition.
#include <stdio.h>
int main() {
int num = 5;
do {
printf("Number is %d\n", num);
num--;
} while (num > 0);
return 0;
}
the do-while loop first executes the block of code, printing the
current value of num, and then decrements num by 1. It
continues looping as long as num is greater than 0.
Q2: explain one and two dimensional array. Write a
program that will 2 arrays as input from user and
print their sum into third array.
One-Dimensional Array:
A one-dimensional array in C is a collection of elements of the
same data type stored in contiguous memory locations. It can be
thought of as a list of elements accessed by a single index.
For example:
int numbers [5]; // Declaration of a one-dimensional array of
integers with size 5
In this array, numbers is a one-dimensional array that can hold 5
integers.
Two-Dimensional Array:
A two-dimensional array in C is an array of arrays. It represents a
grid of elements, where each element is identified by two indices:
row index and column index.
For example:
int matrix [3][3]; // Declaration of a two-dimensional array of
integers with size 3x3
In this array, matrix is a two-dimensional array that can hold 3
rows and 3 columns of integers.
C program that takes two arrays as input from the
user and prints their sum into a third array:
#include <stdio.h>
#define ROWS 3
#define COLS 3
void printArray(int arr[][COLS], int rows) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < COLS; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
}
int main() {
int array1[ROWS][COLS], array2[ROWS][COLS], sum[ROWS]
[COLS];
printf("Enter elements for array1:\n");
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
scanf("%d", &array1[i][j]);
}
}
printf("Enter elements for array2:\n");
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
scanf("%d", &array2[i][j]);
}
}
sumArrays(array1, array2, sum, ROWS);
printf("Sum of arrays:\n");
printArray(sum, ROWS);
return 0;
}
This program first prompts the user to enter elements for two
arrays array1 and array2. Then, it calculates the sum of
corresponding elements and stores them in the sum array. Finally,
it prints the sum array.