0% found this document useful (0 votes)
19 views2 pages

CS 100 - Computational Problem Solving: Fall 2017

This document contains a quiz for a computational problem solving course. It has two questions: 1) Write a function that returns the smallest element in an array. It provides an example of the function that takes in an array, loops through it, and returns the smallest value. 2) Identify what is wrong with a provided loop that is indexing an array and explain two ways to fix it. The loop index starts at 1 instead of 0, causing it to miss the first element of the array. The fixes are to start the index at 0 instead of 1, or reduce the upper bound by 1.

Uploaded by

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

CS 100 - Computational Problem Solving: Fall 2017

This document contains a quiz for a computational problem solving course. It has two questions: 1) Write a function that returns the smallest element in an array. It provides an example of the function that takes in an array, loops through it, and returns the smallest value. 2) Identify what is wrong with a provided loop that is indexing an array and explain two ways to fix it. The loop index starts at 1 instead of 0, causing it to miss the first element of the array. The fixes are to start the index at 0 instead of 1, or reduce the upper bound by 1.

Uploaded by

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

CS 100 – Computational Problem Solving

Fall 2017
Quiz-6

Time: 10 min Total Marks: 10

Name: ___________________________________________

Roll Number: _______________________ Section: __________

a. Write array function that returns the smallest-element in the array. [7]

Example 1 Example 2

5 36 12 27 2 32 29 28 35 72 31 77 68 95 72 87 74 68 85 35
2 31

int smallest_array(int arr[], int size)

int min = arr[0];

for (int i = 1; i < size; i++)

if (arr[i] < min)

min = arr[i];

return min;

}
b. What is wrong with the following loop? Explain two ways of fixing the error.
[3]

int values[10];


for (int i = 1; i <= 10; i++)

{ 


values[i] = i * i;

} 
 


The first way to fix it is to change the “1” to a “0” and the “<=” to “<” in the
loop condition:

int values[10];

for (int i = 0; i < 10; i++) { values[i] = i * i; }

The second way to fix it is to change the “1” to “0” and the “10” to “9” in the
loop condition:

int values[10];

for (int i = 0; i <= 9; i++) { values[i] = i * i; }

You might also like