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...
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:...
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...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
In C++ code. Given an array with 35 integers, print out only those numbers that occur...
In C++ code. Given an array with 35 integers, print out only those numbers that occur more than once. The 35 integers must be read from a txt file (using fstream) or randomly generated (No user input).
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
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 The first letter A and the last letter O
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT