Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

King Hussein Faculty of Computing Sciences

Department of Computer Science

Structured Programming Lab – Section 4


In-Lab Assignment #7 – 2D array and Nested Loop
Monday 4/12/2024 @ 02:00-04:00 PM
Fall 2023/20234

Instructor:
Dr. Ahmad Awwad (a.awwad@psut.edu.jo)

Lab Exercises

Exercise Ex. 1 Ex. 2

Mark /5 /5

Total Mark

Lab #7 Objectives
● 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

Part I: Lab Tasks

Page 1 of 5
Task 1 – Nested Loop output

Q1: What is the output of the following question?

#include <stdio.h>

int main() {
int i, j;
for (i = 1; i <= 10; i++) {
for (j = 1; j <= 10; j++) {
printf("%d x %d = %d\n", i, j, i*j);
}
printf("\n");
}
return 0;
}

Task 2 – Print pattern – Right Triangle


Q2: What is the output of the following question?

#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;
}

Page 2 of 5
Task 3 – Sorting Array-Bubble Sort
Q3: What is the output of the following question?

#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}

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;
}

Page 3 of 5
PART II: Lab Exercises

❖ On the desktop, create a folder named Lab7.

Exercise 1 – Nested Loop


Exercise Objectives
● Nested Loops
● Examples (such as printing patterns)
Problem Description
Inside the “Lab7” folder, create a project named “Lab7Ex1”. Use this project to write and run a C program
that executes the following:

A. Write a C function called PrintShape () takes the following row(int), and a char to draw
with(char) to draw a pyramid.
B. Test the function.

Sample Output

Enter the number of rows for the pyramid: 10


Enter the character to draw with: *

Page 4 of 5
Exercise 2 – Store and Sort a 2D character array
Exercise Objectives
● Sorting arrays (bubble sort only)
● Defining 2D arrays
● Initialize 2D arrays (initializer list and using loops) and printing arrays

Problem Description
Inside the “Lab7” folder, create a project named “Lab7Ex2”.
A. Create a function named fill_array, that takes as argument an 2D array, row and column
of the array. The function should ask the user to enter the values of the array.
B. Create a function named printArray, that takes as argument an 2D array, row and
column of the array, the function should print the array elements in tabular format.
C. Create a function named swap_Raw, that takes as argument an 2D array, row of the
array, column of the array, two integers. The function should swap the raw of the array
according to the two integers sent.
D. Create a function named Sort_Array that takes as argument an 2D array, row of the
array, column of the array, index of the column of type integer. The function should sort
a column of the array according to the index sent in ascending order.
E. Create a main that will test all the functions above.

Sample Output

Fill the array with random numbers.

Printing Array …
15 20 45 33 16
23 49 11 22 38
27 40 17 12 42

swap row 1 with row 2…

Printing Array …
15 45 20 33 16
27 17 40 12 42
23 11 49 22 38

Enter index to sort the row


1

Printing Array …
15 11 20 33 16
27 17 40 12 42
23 45 49 22 38

Page 5 of 5

You might also like