C Programming Looping with row and column

advertisements

Just try to learn and pick up the logic of C programming. I feel little bit confused for the looping to control the row and column

How can i achieve it in C ?

Enter the number: 3
-a-
---
-a-

Enter the number: 5
-a-a-
-----
-a-a-
-----
-a-a-

Thanks for your help!


Since this is a learning exercise, here are some points to get you on the path to a solution:

  • The most common way of iterating in two dimensions is two nested loops
  • In this situation, using two nested for loops would be a good idea
  • Inside the body of the inner for loop you have access to two variables - one denoting the current row, and one denoting the current column
  • Your code needs to decide if a letter or a dash is to be printed based on the values of the row and the column
  • When the row is odd, or when the column is even, print a dash; otherwise, print character 'a'
  • You can tell if a number is odd or even by examining num % 2 or num & 1. In both cases, if the result is zero, the number is even; otherwise, the number is odd.