Question

In: Computer Science

C programming problem I have to design an efficient algorithm which solves this problem. Also, it...

C programming problem

I have to design an efficient algorithm which solves this problem. Also, it needs to include time and space complexities of this algorithm.
There is a matrix (N times N) of integers which rows and columns are sorted in non-decreasing order.
A sorted matrix and integer M will be given and we need to find the position(row,column) of M in this matrix. it's okay to report only one position if there are more than one answers.

when it is 4 times 4 matrix, and integer M is 19

2 29 37 60
9 11 33 87
19 35 17 5
22 15 91 7

the answer will be (3,1)

I need to find an efficient way to find this position

Solutions

Expert Solution

CODE IN C:

#include <stdio.h>

int main()
{
int n;
printf("Enter the size of the square matrix:");
scanf("%d",&n);
int mat[n][n];
printf("Enter the elements of the matrix:");
int i,j;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
scanf("%d",&mat[i][j]);
}
}
printf("below is your matrix:\n");
for(i=0;i<n;i++){
for(j=0;j<n;j++){
printf("%d ",mat[i][j]);
}
printf("\n");
}
int row=0,column=0,number;
printf("Enter an integer:");
scanf("%d",&number);
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(mat[i][j]==number){
row = i+1;
column = j+1;
}
}
}
if(row==0)
printf("\nSorry... Your number didnt exist in your matrix...");
else
printf("\nYour number found at the position (%d,%d)",row,column);
return 0;
}
OUTPUT:


Related Solutions

An algorithm is classically defined as a finite series of steps which solves a problem. What...
An algorithm is classically defined as a finite series of steps which solves a problem. What are some types of instructions which occur in everyday life which would qualify as an algorithm? What are some everyday types of instruction which would NOT qualify as an algorithm
5. Design a dynamic programming algorithm to solve the following problem. Input: An array A[1, ....
5. Design a dynamic programming algorithm to solve the following problem. Input: An array A[1, . . . , n] of positive integers, an integer K. Decide: Are there integers in A such that their sum is K. (Return T RUE or F ALSE) Example: The answer is TRUE for the array A = [1, 2, 3] and 5, since 2 + 3 = 5. The answer is FALSE for A = [2, 3, 4] and 8. Note that you...
Problem 2. Purpose: practice algorithm design using dynamic programming. A subsequence is palindromic if it is...
Problem 2. Purpose: practice algorithm design using dynamic programming. A subsequence is palindromic if it is the same whether read left to right or right to left. For instance, the sequence A,C,G,T,G,T,C,A,A,A,A,T,C,G has many palindromic subsequences, including A,C,G,C,A and A,A,A,A (on the other hand, the subsequence A,C,T is not palindromic). Assume you are given a sequence x[1...n] of characters. Denote L(i,j) the length of the longest palindrome in the substring x[i,...,j]. The goal of the Maximum Palindromic Subsequence Problem (MPSP)...
In C++ or Java Write the Greedy programming Algorithm for the 0-1 Knapsack problem.                    (a)...
In C++ or Java Write the Greedy programming Algorithm for the 0-1 Knapsack problem.                    (a) No fraction allowed Max Weight W= 9 item 1: profit $20, weight 2, prof/weight=$10 item 2: profit $30, weight 5, prof/weight=$6 item 3: profit $35, weight 7, prof/weight=$5 item 4: profit $12, weight 3, prof/weight=$4 item 5: profit $3, weight 1, prof/weight=$3
I have to write an initial algorithm and a refined algorithm for the following problem: Display...
I have to write an initial algorithm and a refined algorithm for the following problem: Display the square of numbers from 0 to some user inputted value. For example if the user enters 3 then the program will need to display the square of 0, 1, 2 and 3. Must use a counter loop. Must use 2 methods. One method that gets the number from the user and returns it. A second method is passed that number as a parameter...
I need C++ programming with output. I have tried other programming and it does not work....
I need C++ programming with output. I have tried other programming and it does not work. So please give me the one that actually works. Assignment 1 Design your own linked list class that works as a template class. It should provide member functions for appending, inserting and deleting nodes. The destructor should destroy the list. The class should also provide a member function that will display the contents of the list to the screen. The class should also provide...
Which Sorting algorithm are in place algorithm and which are not? Is it advantageous to design...
Which Sorting algorithm are in place algorithm and which are not? Is it advantageous to design in place sorting algorithm? How this in place term is related to the time complexity and space complexity of the program?
Provide an algorithm that solves the following problem: input: a binary heap B, an integer value...
Provide an algorithm that solves the following problem: input: a binary heap B, an integer value x output: all keys in B that are smaller than x Constraint: your algorithm must run in time O(K), where K is the number of keys output. Explain why your algorithm has the required runtime behaviour. (Use pseudocode or C++, or any informal (but clear) description. You are free to choose any kind of representation of binary heaps, as long as it was mentioned...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing chess himself to practice his abilities. The chess that Jojo played was N × N. When Jojo was practicing, Jojo suddenly saw a position on his chessboard that was so interesting that Jojo tried to put the pieces of Rook, Bishop and Knight in that position. Every time he put a piece, Jojo counts how many other pieces on the chessboard can be captured...
Write a program in C++ that solves this problem Calculate the area and volume of a...
Write a program in C++ that solves this problem Calculate the area and volume of a sphere problem. Inside a for loop, vary the radius from 10 to 40  with a step or increment of 5 and calculate the area and volume Your radius will be equal to your loop counter. All calculations should have 2 decimal places, but the radius should have zero decimal places and any number of 1,000 or more should have a comma. Print the radius, area,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT