CMP120 - Introduction to Computer Science I: Homework 5 - 2D Arrays and Functions, Assignments of C programming

Homework about Double arrays and loops

Typology: Assignments

2021/2022

Uploaded on 03/12/2023

mariam-gharaibeh
mariam-gharaibeh 🇦🇪

6 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
College of Engineering
Department of Computer Science and Engineering
CMP120 – Introduction to computer science I
Spring 2022
Homework 5: 2D arrays and functions
Date: 24/March/2022
Due: 28/March/2022
Write a C++ program that declares the following 2 arrays:
const int Rs = 8, Cs = 8;
double arr[Rs][Cs];
double arr_quarter[Rs / 4][Cs / 4];
//fill in the array with some data
for (int r = 0; r < Rs; r++)
for (int c = 0; c < Cs; c++)
arr[r][c] = r * 8 + c;
Then:
1. Print out the diagonal of arr
2. Print out the anti-diagonal of arr
3. Downsize arr by a factor of 4 (i.e. Rs/4 and Cs/4) and store the result in arr_quarter. Then print it
out.
4. Then upsize arr_quarter by a factor of four and store the result in arr and print it out.
Expected output:
Print Array:
0 1 2 3 4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31
32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47
48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63
Diagonal elements:
0 9 18 27 36 45 54 63
pf2

Partial preview of the text

Download CMP120 - Introduction to Computer Science I: Homework 5 - 2D Arrays and Functions and more Assignments C programming in PDF only on Docsity!

College of Engineering Department of Computer Science and Engineering CMP120 – Introduction to computer science I Spring 2022 Homework 5: 2D arrays and functions Date: 24/March/ Due: 28/March/ Write a C++ program that declares the following 2 arrays: const int Rs = 8, Cs = 8; double arr[Rs][Cs]; double arr_quarter[Rs / 4][Cs / 4]; //fill in the array with some data for (int r = 0; r < Rs; r++) for (int c = 0; c < Cs; c++) arr[r][c] = r * 8 + c; Then:

  1. Print out the diagonal of arr
  2. Print out the anti-diagonal of arr
  3. Downsize arr by a factor of 4 (i.e. Rs/4 and Cs/4) and store the result in arr_quarter. Then print it out.
  4. Then upsize arr_quarter by a factor of four and store the result in arr and print it out. Expected output: Print Array: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 Diagonal elements: 0 9 18 27 36 45 54 63

Anti-diagonal elements: 7 14 21 28 35 42 49 56 Downsize an array (quarter Rs and Cs): Print Array: 0 4 32 36 Upsize an array (Rs4 and Cs4): Print Array: 0 0 0 0 4 4 4 4 0 0 0 0 4 4 4 4 0 0 0 0 4 4 4 4 0 0 0 0 4 4 4 4 32 32 32 32 36 36 36 36 32 32 32 32 36 36 36 36 32 32 32 32 36 36 36 36 32 32 32 32 36 36 36 36