Question

Analysis of algorithms. C++ pseudo code please

Analysis of algorithms. C++ pseudo code please
media%2Fa9a%2Fa9a1113d-dcbd-41c8-954b-87
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
using namespace std;

int main ()
{
// N*M (5 * 8)
int matrix [5][8] = {
{10, 20, 30, 40, 50, 60, 70, 80},
{9, 19, 25, 39, 49, 55, 66, 79},
{7, 15, 23, 37, 45, 53, 65, 77},
{5, 13, 22, 35, 44, 52, 64, 75},
{4, 12, 21, 31, 43, 51, 63, 73}
};
  
// Size of matrix
int N=5, M=8;
  
// Key we need to find
int KEY = 71;
  
/*
* We First search our key in columns of first row and store that column in col,
* using this we can easily find column which can have our key.
* Here we are keeping row static (i.e first row) and search columns
*/
int col = -1;
int row = -1;
for ( int i=0 ; i < M ; i++ ) {
// As we are traversing column of first row,
//hence we took value of row 0 i.e first row
cout << matrix[0][i];
if(matrix[0][i] >= KEY) {
col = i;
break;
}
}
  
// if col is still -1 means we do not get any column having value less than KEY
if(col > -1) {
/* Now whatever column we get we search our key in that.
* Here we are keeping column static and search through rows.
*/
  
for ( int i=0 ; i < N ; i++ ) {
  
if(matrix[col][i] == KEY) {
row = i;
break;
}
}
  
}
  
if(col > -1 && row > -1) {
cout << "Element found at N: " << row+1 << " and M: " << col+1;
} else {
cout << "No Element found.";
}
  
return 0;
}

Illustration:

To find 71, We first start searching columns of first row and reach last row which gives us complexity of M as there are M columns. As we met our condition at last column the we came to know the element can only be present in this particular column. So we start traversing that particular column of each row and found no such key, here complexity is N as we traverse N rows. This is worst case as we need to traverse every column of first row and every row for that column.

Add a comment
Know the answer?
Add Answer to:
Analysis of algorithms. C++ pseudo code please
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please show me the pseudo code. This pseudo code is used for detect whether a given...

    Please show me the pseudo code. This pseudo code is used for detect whether a given undirected graph contains a cycle: a sequence of nodes that starts and ends in the same node without traversing the same edge twice.

  • Design and Analysis of algorithm - -- pseudo code Write a program that, for a given...

    Design and Analysis of algorithm - -- pseudo code Write a program that, for a given graph, outputs: a. vertices of each connected component b. its cycle or a message that the graph is acyclic

  • Title Algorithms Functional Requirements and Marks using c++ Write a pseudo-code algorithm for the following problems:...

    Title Algorithms Functional Requirements and Marks using c++ Write a pseudo-code algorithm for the following problems: Detect if three angles can make a triangle.    (1) Hint: In a triangle, the sum of all angles is 180. Switch the value of two numbers     (1) If a=2 and b=5, we want to have a=5 and b=2 at the end. Remember that a=b will result in the old value to of a to get lost. Receive a set of numbers and find...

  • ATTENTION!!! Please answer this in details and put briefly explanation. THANK YOU! Just pseudo code. It...

    ATTENTION!!! Please answer this in details and put briefly explanation. THANK YOU! Just pseudo code. It can be C programming Objective is to create pseudo-code to implement a simple cruise control for a car. Write a single function called TestCruisel) in pseudo-code that returns the following conditions .NeedAccelerate .NeedDecelerate . SeedOK which are based on the speed relative to a setpoint DesiredSpeed. You are given the following function GetVehicleSpeed() that returns the speed of the vehicle.

  • (PLEASE UPLOAD ALL PARTS TO THE QUESTION) (IE: PSEUDO CODE AND JAVA CODE TOGETHER NOT ONE...

    (PLEASE UPLOAD ALL PARTS TO THE QUESTION) (IE: PSEUDO CODE AND JAVA CODE TOGETHER NOT ONE OR THE OTHER) Design an Employee class that fields for the following pieces of information: 1) Employee name 2) Employee number Next design a class named ProductionWorker that extends the Employee class. The ProductionWorker class should have fields to hold the following information: 1) Shift number (an integer, such as 1, 2, or 3) 2)Hourly pay The workday is divided into two shifts day...

  • Analysis of algorithms

    This is question from analysis of algorithms class. reperesent the graph below in the list, and print the edges along with their costs at the end. Please us C++. 

  • (PLEASE UPLOAD ALL PARTS TO THE QUESTION) (IE: PSEUDO CODE AND JAVA CODE TOGETHER NOT ONE...

    (PLEASE UPLOAD ALL PARTS TO THE QUESTION) (IE: PSEUDO CODE AND JAVA CODE TOGETHER NOT ONE OR THE OTHER) Design an Employee class that fields for the following pieces of information: 1) Employee name 2) Employee number Next design a class named ProductionWorker that extends the Employee class. The ProductionWorker class should have fields to hold the following information: 1) Shift number (an integer, such as 1, 2, or 3) 2)Hourly pay The workday is divided into two shifts day...

  • Write the pseudo code using modules for the following questions 1.) write pseudo code for a...

    Write the pseudo code using modules for the following questions 1.) write pseudo code for a program that outputs every number from 1 through 20 along with their values doubled and tripled 2.) write pseudo code for a program that outputs every number in reverse order from 25 down to 0

  • Use Java if possible please: Write an algorithm using pseudo code to determine if an undirected...

    Use Java if possible please: Write an algorithm using pseudo code to determine if an undirected graph has any cycle. Analyze the complexity of your algorithm. Write an algorithm using pseudo code to determine if an undirected graph is connected or not. Analyze the complexity of your algorithm. (i) (ii)

  • What is the output of the following pseudo-code segment? A, B, C, or None of the...

    What is the output of the following pseudo-code segment? A, B, C, or None of the above ** In pseudo-code, you ignore syntax errors and focus on the logic ** score = 60 if (score >= 70) print "C" else if (score >= 80) print "B" else if (score >= 90) print "A" and score = 105 if (score >= 90 && score <= 100) print "A" else if (score >= 80) print "B" else print "C"

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT