 November 2019 Solutions Q1 - Grad Plus

# November 2019 Solutions Q1

Q 1 a) What are the six steps of problem solving ? Discuss with example. [06 M]

Ans :

In an OOP concept we can call computer programmers as problem solvers and in order to solve a problem they must :

(1) Represent and understand the data (information) that describes the problem.

(2) Map the steps to use the information to solve the problem.

6 steps are :

(1) Understanding the Problem

(2) Formulating a model

(3) Developing an algorithm

(4) Writing a subsequent program

(5) Testing and improving the program

(6) Evaluating the solution

For Example :

# Write a program to calculate the average of the grades of a student

(1) Understanding the problem :

The question has asked to find the mean of all the given grades of a student.

(2) Formulating a problem :

Average = Grade1 + grade2 + . . . . . + grade N / No of records

(3) Developing a algorithm :

average <- Sum / length (grades_array)

(4) Writing a subsequent program :

A program is written following the above algorithm in the programming language of choice.

(5) Testing and improving the program :

The program is tested against all kinds of data including but not limited to corner cases that may break the algorithm such as the length of the array being returned as 0.

(6) The solution is evaluated and checked whether the output is correct or not.

Q 1 b) Design an algorithm to find out maximum number from an array using the following problem solving strategies : [06 M]
(i) Sequential logic structure
(ii) Decision logic structure
(iii) Loop logic structure.

Ans :

(1) Sequential logic structure :

The most commonly used and the simplest logic structure is the sequential structure. All problems use the sequential, and most problems use it in conjunctions with one or more of the other logic structure.

Flow Chart :

Algorithm : Prog/Algo to find largest (Maximum) number in an array.

Step 1 : Create a local variable max to store the maximum among the list.

Step 2 : Initialize max with the first element initially to start the comparison

Step 3 : Traverse the given array from 2nd element till end and for each element :
– Compare the current element with max
If the current element is greater than max, then replace the value of max with the current element.

Step 4 : At the end, return and print the value of the largest element of array stored in max

//C++ program to find maximum

// in arr[] of size n

#include<bits/stdc FP.h>

Using namespace std;

int largest(int arr[], int n)

{

int i;

//initialize maximum element

int ,ax = arr;

//Traverse array elements

// from second and compare

// every element with current

for (i=1; i<n; i++)

if (arr[i] > max)

max = arr[i];

return max;

}

int main

{

int arr[] = {10, 324, 45, 90, 9808 };

int n = size of (arr)/size of (arr);

cout << "largest(maximum) in given array is"

<< largest (arr, n);

return 0;

}

Output : largest in given array is 9808

(2) Decision Logic Structure :

int largest (int arr[], int n, int)

{

// last index

// return the element

if (i == n-1)

{

return arr[i];

}

// find the maximum from rest of the array

int rcc max = largest (arr, n, i+1);

// Compare with ith element and return

return max (rec max, arr[i];

(3) Loop Logic Structure :

int main()

{

int i, n;

float arr ;

cout << "Enter total number of elements (1 to 100)";

cin >> n;

cout << end;

//store number enter by the user

for(i = 0 ; i < n; i ++)

{

count << "enter number " <<i++ << ":";

cin >> arr[i];

}

//Loop to store largest number to arr

for (i=1 ;i<n ; ++i)

{

//change <to> if you want to find the smallest element

if(arr < arr[i])

arr = arr[i];

}

cout <<endl << "largest element =" <<arr;

return 0;

}

Output :

Enter total number of elements (1 to 100) 8

Enter number 1 : 10.0

Enter number 2 : 20.0

Enter number 3 : 21.0

Enter number 4: 23.5

Enter number 5: 45.5

Enter number 6 : 55.5

Enter number 7 : 61.7

Largest element : 53.5

Scroll to Top
error: Alert: Content selection is disabled!!