Question

If we are given start row, start column, endrow, endcolumn. How to print a diagonal line...

If we are given start row, start column, endrow, endcolumn. How to print a diagonal line in an array using ONLY while loop in C?  Print a 4 in the diagonal position.

Homework Answers

Answer #1
#include<stdio.h>

void space(int n)
{
    for(int i=0;i<n;i++)printf(" ");
}

void southeast(int m[][3],int s_row,int s_col,int e_row,int e_col)
{
    int temp_col=s_col;
    int temp_row=s_row;
    while(temp_row<=e_row)
    {
        space(temp_row-s_row);
        printf("%d\n",m[temp_row][temp_col]);
        temp_row++;
        temp_col++;
    }
}

void northwest(int m[][3],int s_row,int s_col,int e_row,int e_col)
{
    int temp_col=e_col;
    int temp_row=e_row;
    while(temp_row<=s_row)
    {
        space(temp_row-e_row);
        printf("%d\n",m[temp_row][temp_col]);
        temp_row++;
        temp_col++;
    }
}

void northeast(int m[][3],int s_row,int s_col,int e_row,int e_col)
{
    int temp_col=e_col;
    int temp_row=e_row;
    while(temp_row<=s_row)
    {
        space(s_row-temp_row);
        printf("%d\n",m[temp_row][temp_col]);
        temp_row++;
        temp_col--;
    }
}

void southwest(int m[][3],int s_row,int s_col,int e_row,int e_col)
{
    int temp_col=s_col;
    int temp_row=s_row;
    while(temp_row<=e_row)
    {
        space(e_row-temp_row);
        printf("%d\n",m[temp_row][temp_col]);
        temp_row++;
        temp_col--;
    }
}

int main()
{
    int m[][3]={{1,2,3},{4,5,6},{7,8,9}};
    int s_row=2,s_col=0;
    int e_row=0,e_col=2;
    if(s_row<e_row && s_col<e_col)southeast(m,s_row,s_col,e_row,e_col);
    else if(s_row>e_row && s_col>e_col)northwest(m,s_row,s_col,e_row,e_col);
    else if(s_row>e_row && s_col<e_col)northeast(m,s_row,s_col,e_row,e_col);
    else 
        southwest(m,s_row,s_col,e_row,e_col);

    return 0;
}

Output

One thing I could not understand what does "Print a 4 in the diagonal position." mean? Tell me in the comment.

Know the answer?
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
Similar Questions
Let Aequals=left bracket Start 2 By 2 Matrix 1st Row 1st Column 1 2nd Column 2...
Let Aequals=left bracket Start 2 By 2 Matrix 1st Row 1st Column 1 2nd Column 2 2nd Row 1st Column 8 2nd Column 18 EndMatrix right bracket 1 2 8 18 ​, Bold b 1b1equals=left bracket Start 2 By 1 Matrix 1st Row 1st Column negative 5 2nd Row 1st Column negative 36 EndMatrix right bracket −5 −36 ​, Bold b 2b2equals=left bracket Start 2 By 1 Matrix 1st Row 1st Column 3 2nd Row 1st Column 16 EndMatrix right...
Given: Ho: Row effect is not significant H1: Row effect is significant Ho: Column effect is...
Given: Ho: Row effect is not significant H1: Row effect is significant Ho: Column effect is not significant H1: Column effect is significant Ho: Interaction effect is not significant H1: interaction effect is significant # row column X 1 A M 6 2 A M 12 3 A M 6 4 A F 5 5 A F 6 6 A F 5 7 B M 3 8 B M 2 9 B M 3 10 B F 17 11 B...
Uses a while loop to print the numbers from 3 - 19. Uses a do-while loop...
Uses a while loop to print the numbers from 3 - 19. Uses a do-while loop to print the numbers from 42 - 56. Uses a for loop to print the numbers from 87 - 95. Asks the user for 2 numbers. Uses a loop to print all numbers between the given numbers, inclusive. Note: Consider that your user's second number can be lower! (see example below) Note: Also consider that your user might give you two of the same...
The following table contains observed frequencies for a sample of 200. Row Variable Column Variable A...
The following table contains observed frequencies for a sample of 200. Row Variable Column Variable A B C P 24 48 54 Q 26 22 26 Test for independence of the row and column variables using α = 0.05. State the null and alternative hypotheses. H0: The column variable is independent of the row variable. Ha: The column variable is not independent of the row variable.H0: Variable P is not independent of variable Q. Ha: Variable P is independent of...
The following table contains observed frequencies for a sample of 240. Row Variable Column Variable A...
The following table contains observed frequencies for a sample of 240. Row Variable Column Variable A B C P 15 25 15 Q 30 60 25 R 15 20 35 Test for independence of the row and column variables using α = 0.05. State the null and alternative hypotheses. H0: Variable P is not independent of variables Q and R. Ha: Variable P is independent of variables Q and R.H0: Variable P is independent of variables Q and R. Ha:...
Determine the memory address for the following element using row-major order. Show the math. Given, the...
Determine the memory address for the following element using row-major order. Show the math. Given, the base address of the array is 1200. The array is 9 rows, 12 columns. Element size is 4. What is the memory address of Array[9,11]? for data structure class
Write a C++ Program to print the first letter of your first and last name using...
Write a C++ Program to print the first letter of your first and last name using stars. Note: 1) Using nested For Loop 2) The number of lines is given by user. 3) Using one Outer loop to print your letters. 4) Print the letters beside each other my name's Aya amro
Let's say you have a 4 row, 7 column Matrix A that is linearly dependent. How...
Let's say you have a 4 row, 7 column Matrix A that is linearly dependent. How do you find a subset of A that is a basis for R4 and why that set forms a basis for R4?
Create a program to calculate and print basic stats on a set of a given input...
Create a program to calculate and print basic stats on a set of a given input values representing students' scores on an quiz. 1) Ask the user for the total number of quiz scores to be input (assume a positive integer will be given). 2) Create an array to hold all the quiz scores and then get all the scores from input to put into the array. For example, if the user input 10 in step (1) then you should...
CODE BLOCK C computer_num == 7 print("Guess a number form 1 to 10") # set users...
CODE BLOCK C computer_num == 7 print("Guess a number form 1 to 10") # set users guess value to zero so we enter the loop guess = 0 while guess == computer_num guess = input ("Enter a guess (1-10)") print("Correct the number is "+str(computer_num)) How many times will “Correct the number is…” print when this code block is executed?.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT