0% found this document useful (0 votes)
58 views4 pages

InLab12Exercise cs-100 Lums

This document outlines the instructions for Lab 12, which involves multi-dimensional arrays and pointers. It is due by 12:25PM on Monday 29 November 2021. Students must submit a zip file of their work labeled with their name and roll number. Communication between students and using outside help is prohibited. The tasks involve (1) checking if a matrix is a "magic square" by summing the rows, columns, and diagonals, and (2) calculating the dot product and Euclidean distance between two vectors using pointers to access array elements. Failing to follow file naming or coding conventions will result in point deductions. The objective is to demonstrate a deep understanding of computational concepts.

Uploaded by

muhammad mujeeb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views4 pages

InLab12Exercise cs-100 Lums

This document outlines the instructions for Lab 12, which involves multi-dimensional arrays and pointers. It is due by 12:25PM on Monday 29 November 2021. Students must submit a zip file of their work labeled with their name and roll number. Communication between students and using outside help is prohibited. The tasks involve (1) checking if a matrix is a "magic square" by summing the rows, columns, and diagonals, and (2) calculating the dot product and Euclidean distance between two vectors using pointers to access array elements. Failing to follow file naming or coding conventions will result in point deductions. The objective is to demonstrate a deep understanding of computational concepts.

Uploaded by

muhammad mujeeb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CS100 Computational Problem Solving

Fall 2021-22

Deadline: 12:25PM Monday 29 November2021

In-Lab 12: Multi-Dimensional Arrays & Pointers

(Lab Section 3) Total Marks = 60

Lab Guidelines

1. You must put all your work into a folder named Lab03_YourRollNo (e.g,
Lab12_23100268), compress it into a “zip” file and submit the zip file on LMS
(Assignment -> Lab12) before Monday, 29th Sept 12:25 PM. No late submissions or email
submissions will be accepted.
2. Communicating with each other is NOT permitted. Searching for answers or help on the
internet is not permitted. All help material provided in the Lab Manual is allowed. If you
have a question, ask the TAs on Slack in your lab section channel.
3. The object is not simply to get the job done, but to get it done in the way that is asked for in
the lab. The objective is not just to get the code working, but to have a deep understanding
of why it is working.
4. Any cheating case will be reported to the Disciplinary Committee without any delay.
5. Failure to adhere to the naming convention of the file will result in 2.5 marks deduction.
6. Failure to adhere to coding conventions of the file will also result in 2.5 marks deduction.

Coding Conventions:
● Constants are “ALLCAPS” or “ALL_CAPS”.
● Variables are “allsmall” or “all_small”.
● All function names must be “firstWordSmallAllOtherWordsCamelCase”.
● All curly brackets defining a block must be vertically aligned.
● File naming: Lab04_YourRollNo_AssignedTA

Learning Objective:
● Use of integer and double data types
● Practice mathematical operations
Task 1: Magic Squares [30 marks]
An 𝑛 × 𝑛 matrix is called a magic square if

• The sum of all values of a row is the same value (𝑥) for every row of the square
• The sum of all values of a column is also the same value (𝑥) for every column
• The sum of the two diagonals is also the same value (𝑥) for both diagonals
For example, the following is a magic 4 × 4 matrix:

Notice that the sum of all numbers of the first row 16 + 3 + 2 + 13 = 34. The sum of all
numbers of the second row 5 + 10 + 11 + 8 = 34. The sum of all values of the first column
16 + 5 + 9 + 4 = 34. The sum of all values of the first diagonal 16 + 10 + 7 + 1 = 34.
Write a program that has a 4 × 4 or 3 × 3 or 5 × 5 or any square matrix. You can hard-code the
values of the array. Now check if the matrix is a magic matrix. Assume that the matrix can only
have positive numbers in it.
Note: Your code must be general and must work on any square matrix, even if it is 100 × 100.
Hint: You can come up with your own algorithm if you want. But if you’re confused, here is one
good way to do it:
You can create three functions:

• one function to which you pass your array and it checks if each row has the same sum
(and returns that sum),
• the second function to which you pass your array and it checks if each column has the
same sum (and returns that sum), and
• the third function that checks if both diagonals have the same sum (and returns that
sum).
In the main function, call each function, pass them the array, and then compare the sum
that they return, to check if their returned sums are all equal to each other. If yes, then the
matrix is magic, otherwise it is not. Breaking down your code into functions makes it easier
to manage, understand, and debug. If you write everything into one big fat main function,
the code will get very complex very fast.
Don’t just write code, write quality code.
______________________________________________________________________________
Task 2: Dot Product [30 Marks]

NOTE: You will get zero marks if you don’t use pointer arithmetic for accessing
the array elements in this task. You cannot use arr[x][y] notation. Use pointer
arithmetic.
In this task we will be using arrays to calculate the dot product and the euclidean distance
between two vectors. Arrays are similar to that of vectors and in this task first, you would have
to first take input for the size of the arrays and then get the corresponding entries in the
vectors from the user.
Then using the dot product formula which is given below:
𝑛

𝑎. 𝑏 = ∑ 𝑎𝑖 𝑏𝑖 , 𝑤ℎ𝑒𝑟𝑒 𝑛 𝑖𝑠 𝑡ℎ𝑒 𝑠𝑖𝑧𝑒 𝑜𝑓 𝑦𝑜𝑢𝑟 𝑎𝑟𝑟𝑎𝑦


𝑖=0

Here is an example:

You will caluclate the dot product and display it to the user.
You will then calculate the Euclidean distance between the vectors using the formula given
below:

𝑒𝑑(𝑝, 𝑞) = √∑(𝑞𝑖 − 𝑝𝑖 )2 , 𝑤ℎ𝑒𝑟𝑒 𝑛 𝑖𝑠 𝑡ℎ𝑒 𝑠𝑖𝑧𝑒 𝑜𝑓 𝑦𝑜𝑢𝑟 𝑎𝑟𝑟𝑎𝑦


𝑖=0

Sample Output on the next page ->

The output should look like the following:


NOTE: You will get zero marks if you don’t use pointers for accessing the array
elements
Grading Breakout:

-> 15 marks for correct input using pointer indexing


-> 15 marks for correct dot product calculation

Best of Luck

You might also like