Draw the Flow Control Diagram for the following piece of code.
insertion_procedure (int a[], int p [], int N)
{
int i,j,k;
for (i=0; i<=N; i++)
p[i] = i;
for (i=2; i<=N; i++)
{
k = p[i];
j = 1;
while (a[p[j-1]] > a[k])
{
p[j] = p[j-1];
j—-;
}
p[j] = k;
}
}
Can someone show me how to draw a control flow graph as I am unfamiliar. Also if you can't help don't respond with a stupid answer. This question is relation to an exam I have coming up in Software Testing. Thank You
Related
#include<iostream>
using namespace std;
int main()
{
int arr[] = {40,20,14,20,55,14,22,45,22,447,441,224,421,2,14,1,9};
int size = sizeof(arr) / sizeof(int);
for (int i = 0; i < size; i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
for (int i = 0; i < size; i++)
{
int Index_of_Min = i;
for (int j = i+1; j < size; j++)
{
if (arr[j] < arr[Index_of_Min])
{
Index_of_Min = j;
}
swap(arr[Index_of_Min], arr[i] );
}
}
for (int i = 0; i < size; i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
return 0;
}
in the above program when we run it the number 9, 224 are not getting sorted
i want to understand why the program is not working anyone who knows the solution then your help is much appreciated
and please explain me what was my mistake so i will not repeat it again.
Thank You
Saw the exact same error only a few days ago. Your loop is wrong, specifically the swap is in the wrong place. This is how it should look
for (int i = 0; i < size; i++)
{
int Index_of_Min = i;
for (int j = i+1; j < size; j++)
{
if (arr[j] < arr[Index_of_Min])
{
Index_of_Min = j;
}
}
swap(arr[Index_of_Min], arr[i] );
}
You do the swap after the inner loop has found the index of the minimum element, not while it is finding that index.
In the code below, my aim is to multiply two matrices reflect[3][3] and mat[3][s] where s can be any value 0-10. Here the statement (A) and (B) is not getting printed, please tell me why??
#include<iostream>
# include<math.h>
#include<conio.h>
using namespace std;
int mat[10][10];
int result[3][10];
int reflect[3][3]= {1,0,5,0,1,5,0,0,1};
int i , j,k,s;
void multiply_matrix(int A[3][3], int B[3][10])
{
for(i=0; i<3; i++)
for( j=0; j<10; j++)
result[i][j] = 0;
for (i = 0; i < 3; i++)
{
for (j = 0; j < s; j++)
{
result[i][j] = 0;
for (k = 0; k < 3; k++)
{
result[i][j]=result[i][j]+(A[i][k]*B[k][j]) ;
}
cout<<result[i][j]<<" ";//------(1)
}
cout<<endl;
}
cout<<"Multiplication after matrix: "<<endl;
for(i=0; i<3; i++)
{
for(j=0; j<s; j++)
{
cout<<result[i][j]<<" ";//------(B)
}
cout<<endl;;
}
}
int main()
{
int i, j,s;
cout<<"Enter the sides of polygon :\n";
cin>>s;
cout<<"Enter the coordinates of polygon :\n";
cout<<"Enter x coordinates ";
for(i=0; i<s; i++)
cin>>mat[0][i];
cout<<"Enter y coordinates ";
for(i=0; i<s; i++)
cin>>mat[1][i];
cout<<"\n\n";
for(i=0; i<s; i++)
mat[2][i] = 1;
cout<<"MAt: "<<endl;
for(i=0; i<3; i++)
{
for(j=0; j<s; j++)
{
cout<<mat[i][j]<<" ";
}
cout<<endl;
}
multiply_matrix(reflect, mat);
cout<<"End"<<endl;
return 0;
}
I have attached a sample output image for reference:
Output of code
I am a newbie and have tried various things but I am just not able to figure my mistake. Thanks in advance for your help.
You have two s in your code. Globally:
int i , j,k,s;
And in main:
int i, j,s;
The one in main you assign a value read from user input, but the global one is 0 always. multiply_matrix uses the global one, hence this loops have zero iterations:
for (j = 0; j < s; j++)
{
result[i][j] = 0;
for (k = 0; k < 3; k++)
{
result[i][j]=result[i][j]+(A[i][k]*B[k][j]) ;
}
cout<<result[i][j]<<" ";//------(1)
}
and
for(j=0; j<s; j++)
{
cout<<result[i][j]<<" ";//------(B)
}
I have working code for rotating 2D matrix in clockwise direction but I'm having TLE (time limit exceeded) problem when k reaches big numbers. I don't know how to simplify my code, I'm guessing the for cycles are causing the problem but I can't see a way to make my code work without them. Is there any other way to simplify my code to avoid TLE?
#include <iostream>
#include <fstream>
using namespace std;
void funk(int a[][101], int n,int k);
int main()
{
int a[101][101],n,k;
ifstream ived;
ived.open("15.txt");
ived>>n>>k;
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
ived>>a[i][j];
}
}
ived.close();
funk(a, n, k);
ofstream isved;
isved.open("15rez.txt");
for (int i=0;i<n; i++) {
for (int j=0; j<n; j++) {
isved<<a[i][j]<<" ";
}
isved<<endl;
}
isved.close();
return 0;
}
void funk(int a[][101], int n, int k) {
for (int pak=0; pak<k; pak++) {
for (int i=0; i<n/2; i++) {
for (int j=i; j<n-i-1; j++) {
int prad=a[i][j];
a[i][j] = a[n-1-j][i];
a[n-1-j][i] = a[n-1-i][n-1-j];
a[n-1-i][n-1-j] = a[j][n-1-i];
a[j][n-1-i]=prad;
}
}
}
}
If k is the number of times the matrix should be rotated, then you can use the fact that after 4 rotations it transforms into itself. Hence, the result will be the same if you replace k with k % 4, thereby replacing O(k) algorithm with O(1) one.
I have this piece of code:
for (int i=0; i<N; i++)
{
int j;
if (i%2==1) j=1;
else j=0;
for (; j<N; j+=2)
{
for(int k=0; k<N; k++)
{
int n;
if (k%2==1) n=1;
else n=0;
for (; n<N; n+=2)
{
for (int l=0; l<N; l++)
{
int o;
if (l%2==1) o=1;
else o=0;
for (; o<N; o+=2)
{
for(int m=0; m<N; m++)
{
int p;
if (m%2==1) p=1;
else p=0;
for (; p<N; p+=2)
{
if (check_full(lenta,i,j,k,n,l,o,m,p))
{
count++;
cout<<"Lenta uzsipilde: ("<<i<<","<<j<<"), "<<"("<<k<<","<<n<<"), "<<"("<<l<<","<<o<<"), "<<"("<<m<<","<<p<<"), "<<endl;
}
}
}
}
}
}
}
}
}
Is there any way it could be turned into recursion? Basically these loops find all possible coordinates for given problem. And if it can be converted into one small recursion, will I need to use array instead of 8 variables?
Here's what I tried to do, but it doesn't work:
void findBishops(){
for (int i=0; i<N; i++){
int j;
if (i%2==1) j=1;
for (; j<N; j+=2){
putIntoArray(array, i, j);
if (isFull(board, array)){
PrintAnswer(array);
}else{
arrayCount = arrayCount-2;
findBishops();
}
}
}
}
void putIntoArray(array[], i, j){
array[arrayCount++] = i;
array[arrayCount++] = j;
}
I would probably recurse over the bishops rather than loop over the board:
First place one bishop, then recursively place the second, and so on until you reach the last.
The backtracking happens when you return from the recursion, and that's when you try the next alternative, for a single bishop, and recurse again.
Until you've run out of options - that's when you're done.
Here's a rough outline:
place_bishop(this_bishop)
if this_bishop is the final bishop:
for every possible position of this_bishop:
see if it's a solution and handle that
else:
for every possible position of this_bishop:
place_bishop(next_bishop)
Position-picking requires some thought in order to not locate the same solution more than once.
I am aware that there allready are similar questions here but no answer really helped me.
This is my problem:
I have given an array with 512x512 pixels in it. Each pixel has a value like 165.88009. ( I have to create a heatmap in GnuPlot later)
Now I want to "smoothen" it by creating the average of a variable block of pixels (like 4-16) and write it into a new 2D array and jump to the next block until it is done.
The size of the array should stay the same. So if I average 4 pixels those 4 pixels get the new value.
I made a function for this but it doesn't work properly.
Calculating the average is not my problem. The problem is that I want to have a variable width of pixels but I don't know how to make my algorithm jump to the next block.
Im not experienced in C++ and maybe I have to do it completely different.
So any help or inspiration is greatly appreciated :)
here is my code:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int i, j, m, n, k;
void Average(double **Data, int width) // width gets defined and initiated in main
{
double sum;
double avg;
fstream Output;
Output.open( "GemittelteWerte.dat", ios::out);
double** IV_Matrix = new double* [m];
for (int i=0; i<m; i++)
{
IV_Matrix[i] = new double [n];
}
for (int i=0; i<m; i++)
{
for (int j=0; j<n; j++)
{
IV_Matrix[i][j] = 1.0;
}
}
// Here start all my troubles:
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j+=width)
{
sum = 0.0;
k=j;
for( k; k<(j+width); k++)
{
sum+=Data[i][k];
}
avg=(sum/width);
for (int k; k<(j+width); k++)
{
IV_Matrix[i][k] = avg;
}
}
}
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
Output<<setprecision(10)<<IV_Matrix[i][j]<<"\t";
}
Output<<"\n";
}
Output.close();
}
Is this block a 2D block (4 = 2x2, 16 = 4x4)? You simply want to do a 2D convolution? Then better use odd widths with 3x3, 5x5, ... kernels.
// int x, y are the dimensions of your image
double get (double **img, int i, int j) // zero padding for areas outside image
{
if (i<0 || i>=x || j<0 || j>=y)
return 0;
else
return img[i][j];
}
void conv (double **img, double **result, int width2) // kernel is (2*width2+1)^2
{
double sum;
for (int i=0; i<x; i++)
for (int j=0; j<y; j++)
{
sum = 0;
for (int ii=-width2; ii<=width2; ii++)
for (int jj=-width2; jj<=width2; jj++)
sum += get(img,i+ii,j+jj) / ((2*width2+1)*(2*width2+1));
result[i][j] = sum;
}
}
This smoothes img to result. Its however the slow unseparated solution. For small images and kernels no problem.
Edit: then easier:
// x, y are the dimensions of your image (x rows, y colums)
void avg (double **img, double **result, int width) // width must be >= 1 and
{ // should be a divider of y
double sum;
for (int i=0; i<x; i++) // process all rows
{
for (int j=0; j<y; j+=width) // jump in block width through a row
{
sum = 0.0;
for (int w=0; w<width; w++) // calculate average of a block
{
sum += img[i][j+w] / width;
}
for (int b=0; b<width; b++) // write average in each pixel inside block
{
result[i][j+b]= sum;
}
}
}
//di means diagonal index
for(int di = 0; di < n/width; ++di) {
int sum = 0.0;
//we sum the values
for(int i = di*width; i < (di+1)*width; ++i)
{
for(int j = di*width; j < (di+1)*width; ++j)
{
sum += Data[i][j];
}
}
//Divide by the number of values
sum /= width*width;
//Spread the results
for(int i = di*width; i < (di+1)*width; ++i)
{
for(int j = di*width; j < (di+1)*width; ++j)
{
IV_Matrix[i][j];
}
}
}
//n might not be a multiple of width
if(n % width != 0) {
//we sum the values
for(int i = (n/width)*width; i < n; ++i)
{
for(int j = di*width; j < (di+1)*width; ++j)
{
sum += Data[i][j];
}
}
//Divide by the number of values
sum /= width*width;
//Spread the results
for(int i = (n/width)*width; i < n; ++i)
{
for(int j = (n/width)*width; j < n; ++j)
IV_Matrix[i][j];
}
}
}