Week 7 Worksheet
Week 7 Worksheet
….
#include <stdio.h>
#include <stdlib.h>
void print1D( int a[],int size){
for(int i=0;i<size;i++)
printf("%d ,",a[i]);
}
void print2D(int a[][3],int rz){
for(int i=0;i<rz;i++){
for(int j=0;j<3;j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
}
int max(int a[],int s){
int m=a[0];
for(int i=0;i<s;i++){
if(a[i]>m)
m=a[i]; }
return m;
}
int main(void) {
int r[8]={1,4,6,7,18,4,3,1};
int m[100];
for(int i=0;i<100;i++){
m[i]=rand()%6;
}
print1D(r,3);
printf("\n**************\n");
print1D(m, 20);
printf("\n**************\n");
printf("maximum of m=%d\n",max(m,8));
int grades[100][3];
for(int i=0;i<100;i++){
for(int j=0;j<3;j++)
grades[i][j]=35+rand()%66;
}
print2D(grades,5);
for(int r=0;r<100;r++){
grades[r][0]=100;
}
printf("\n**************\n");
print2D(grades,5);
printf("\n**************\n");
return 0;
}
Week 7
● Nested Loops
● Examples (such as printing patterns)
● Sorting arrays (bubble sort only)
● Defining 2D arrays
● Initialize 2D arrays (initializer list and using loops) and printing arrays
● Nested Loops
Q1 :What is the output of the following question :
#include <stdio.h>
int main() {
int i, j;
printf("\n");
return 0;
Q2 :Given an array of integers, find all pairs of elements whose sum is equal to a given value.
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int i, j, sum = 0;
for (i = 0; i < 5; i++) {
for (j = i+1; j < 5; j++) {
sum += arr[i] + arr[j];
}
}
printf("The sum of all pairs of elements is: %d\n", sum);
return 0;
}
Q3 pattern printing like
Right tringle
#include <stdio.h>
int main() {
int rows = 5;
int i, j;
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;}
#include <stdio.h>
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: \n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Consider a 2D grid of size 4 x 5, where each cell contains a cost. A game character has to get from
the top left corner to the bottom right corner moving either to the right cell or to the cell below:
If the right cell has a lower cost, the character moves to the right.
Otherwise, the character moves to the cell below.
Write a complete C program to simulate the movement of this game character.
1. Define a function named show_path that receives the 2D grid as an argument. This function
must print the path (as shown in the sample output) and return the cost of the path.
2. Define a main() function that:
○ Defines a 2D array and fills it with random integers between 0 and 9 inclusive.
○ Calls function show_path to print the path and then prints the cost of the path.
Alternatives.
● Start at the bottom right corner and move to the top left corner (only up and right
allowed)
● Start at the top right corner and move to the bottom left corner (only down and left
allowed)
● Start at the bottom left corner and move to the top right corner (only up and right
allowed)
Grading.
● [3 points] main(): 2 for defining and filling the 2D array + 1 for calling the function and
printing.
● [7 points] show_path():
○ Loop condition: 1 point
○ Each if statement: 1 point
○ Updating the sum: 1 point
○ 0.5 for initializing the variables and 0.5 for returning the sum.
Answer
#include <stdio.h>
#include <cstdlib>
#include <ctime>
#define ROWS 4
#define COLS 5
if (i == ROWS-1) {
j++; printf("right ");
}
else if (j == COLS-1) {
i++; printf("down ");
}
else if (a[i+1][j] < a[i][j+1]) {
i++; printf("down ");
}
else {
j++; printf("right ");
}
}
printf("\n");
return sum;
}
int main() {
srand(time(0));
int a[ROWS][COLS];
for (int i = 0; i < ROWS; i++)
for (int j = 0; j < COLS; j++)
a[i][j] = rand() % 10;
printf("path = ");
printf("cost = %d\n", shortest_path(a));
return 0;
}
int a[SIZE][SIZE];
int c = 1;
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (j == 0)
a[i][j] = -1;
else
a[i][j] = c;
c++;
}
}
Assuming that SIZE = 100, what are the contents of each of the following array cells after the
above code finishes execution?
a[0][99] =
a[99][0] =
a[99][99] =
Alternatives. Use other values for SIZE, like: 200, 300, 400, etc.
Question 3
Write a program that reads a non-negative integer n. For each number x from 1 to n inclusive, the
program must print all the positive divisors ( عوامل/ )قواسمof x. If the user enters a negative number,
the program should not do anything.
Enter a number: 10
Divisors of 1: 1
Divisors of 2: 1 2
Divisors of 3: 1 3
Divisors of 4: 1 2 4
Divisors of 5: 1 5
Divisors of 6: 1 2 3 6
Divisors of 7: 1 7
Divisors of 8: 1 2 4 8
Divisors of 9: 1 3 9
Divisors of 10: 1 2 5 10
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
if (n < 0)
return 0;
if (i % j == 0)
printf("\n");
return 0;
Question 4
Implement function int check(int a[], int size), which returns 1 if any of the elements in the first
half of the given array is also in the second half. The function returns 0 otherwise.
You can assume that the array size is even (no need to check for this).
Example 1.
Example 2.
Solution.
return;
}
return;
}
printf("no");
Question 6 2D Arrays
PART 1.
0 0 0 # # # # # 0 0 1 0 2 # # # # # 2 1
# 0 # # 0 0 0 0 # # # 2 # # 3 1 1 3 # #
0 0 0 0 # 0 0 # # 0 1 1 2 2 # 1 1 # # 2
0 # # 0 # 0 0 0 0 0 1 # # 3 # 1 1 1 2 0
0 # 0 # 0 0 # 0 # 0 1 # 4 # 3 2 * 3 # 1
0 0 # # # # # # # 0 0 2 # # # # # # # 1
0 0 # # 0 0 0 # # 0 0 1 # # 2 1 2 # # 1
Example.
Solution.
#define ROWS 7
#define COLS 10
Write a complete C program that creates a 2D array of size 7 x 10 , fills it randomly with
either '0' or '#' and sends it to function count(…). The program should then print the
array after the function call (in the same format as the sample in part 1).
Solution.
int main() {
char a[ROWS][COLS];
count(a);
printf("\n");
}
return 0;
}
Question 7: [8 Points]
Write a C function called RotateLine90. The function receives a (2D) two dimensional array of
type integer of size 5 by 5, also the function receives coordinates of line points. The function
should have the following signature:
void RoateLine90( int A[][5], int x1, int y1, int x2, int y2)
The integer array represents an image that has two colors (black and white). Each array cell of
value 1 represents black color and each array cell of value 0 represents white color. The image
can have only horizontal vertical lines. Function is supposed to rotate the given line by 90
degrees counter clockwise.
For example, below is an image of 5 by 5 with two horizontal lines, the left side shows the
image and the right side shows the equivalent matrix of it:
0 0 0 0 0
0 0 0 0 0
0 0 1 1 1
0 0 0 0 0
1 1 1 1 0
Calling the function RoateLine90(A,2,2,2,4) will result in rotating the line from (2,2) to (2,4) by
90 degrees. The final shape should be
0 0 1 0 0
0 0 1 0 0
0 0 1 0 0
0 0 0 0 0
1 1 1 1 0
#include <stdio.h>
void RotateLine90(int A[][5], int x1, int y1, int x2, int y2) {
return;
int dx = x2 - x1;
int dy = y2 - y1;
A[x1+i][y1+j] = A[x1+j][y1+i];
A[x1+j][y1+i] = tmp;
}
// Flip the line if it was horizontal (top to bottom)
if (y1 == y2) {
A[i][j] = A[x2-(i-x1)][j];
A[x2-(i-x1)][j] = tmp;
if (x1 == x2) {
A[i][j] = A[i][y2-(j-y1)];
A[i][y2-(j-y1)] = tmp;
int main() {
{0, 0, 1, 0, 0},
{0, 0, 1, 0, 0},
{0, 0, 1, 0, 0}};
printf("\n");
return 0;
Question 8
a) How many A’s, B’s and C’s will the following piece of code print?
Provide your answer on three lines as follows:
A = {insert a number here}
B = {insert a number here}
C = {insert a number here}
For each of the pieces of code below, show the output in the given box.
(b)
(c)
int i = -1;
do {
i++;
switch (i) {
case 0:
printf("Zero\n");
break;
case 1:
case 2:
case 3:
i > 2 ? printf("Three\n") : printf("One or Two\n");
break;
case 6:
printf("Six\n");
case 10:
printf("Ten\n");
break;
default:
printf("Other\n");
}
} while (i < 10);
Question 9
Write a program that reads a number N and outputs the shape of a tornado of size N. The following are
examples:
******* **** *
******* **** *
****** ***
****** ***
***** **
***** **
**** *
**** *
***
***
**
**
*
*