I am attempting to process a two-dimensional array in C. I tried two nested for loops, but it seems that the two loops execute separately. I expect that the inside loop loops eight times for each loop of the outside loop, resulting in eight times the number of outside loops being the total number of loops.
As a simplified test, I tried this:
#include <stdio.h>
int main() {
int x = 0;
int y = 0;
for (; x < 7; x++, printf("(%d,%d)", x, y)) {
for (; y < 8; y++, printf("(%d,%d)", x, y)) { }
}
}
This resulted in these results:
(0,1)(0,2)(0,3)(0,4)(0,5)(0,6)(0,7)(0,8)(1,8)(2,8)(3,8)(4,8)(5,8)(6,8)
Could somebody please explain to me why this might be happening? Thanks.
The reason is simple: you do not reinitialize the y variable in inner loop: when it reaches 8, it stays this way and the inner loop does not execute any more. Change your code to:
#include <stdio.h>
int main() {
for (x=0; x < 7; x++, printf("(%d,%d)", x, y)) {
for (y = 0; y < 8; y++, printf("(%d,%d)", x, y)) { }
}
}
Related
I made a code wherein I should print the square and cube of the first 10 counting numbers but I used for loops. What I'm looking for is how to print the same output but using nested (for) statement.
Here is my code:
#include<stdio.h>
int main()
{
int x;
printf("x\tx*x\tx*x*x\t\n");
for(x=1; x<=10; x++)
printf("%d\t%d\t%d\n", x, x*x, x*x*x);
return 0;
}
With these few items to print, a nested for loop seems unecessary, but if you really want one, it could look like this:
for(x = 1; x <= 10; x++) {
for(int i = 0, X = x; i < 3; ++i, X *= x) {
printf("%d\t", X);
}
putchar('\n');
}
Demo
Here I have added the solution of this code along with its output using the nested for loop
I'm having problems explaining this code and I would really like a detailed explanation of how it works.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x=-3, y=0, k,j;
for( k=j=-3 ; x= x+(k<j),++j ; y+=2);
printf("x=%d y=%d k=%d j=%d",x,y,k,j);
getch();
}
By the way, the answers are x=-1 y=4 k=-3 j=0. The problem is that I have tried Explaining it and I don't actually know how to get past the increment part (++j).
int x=-3, y=0, k,j;
for( k=j=-3 ; x= x+(k<j),++j ; y+=2);
int x=-3, y=0, k=-3, j=-3;
while(x = x+(k<j), ++j) {
y += 2;
}
int x=-3, y=0, k=-3, j=-3;
x += k<j;
while(++j) {
y += 2;
x += k<j;
}
int x=-3, y=0, k=-3, j=-3;
x += 0;
while(++j) {
y += 2;
x += k<j;
}
int x=-3, y=0, k=-3, j=-3;
while(++j) {
y += 2;
x += k<j;
}
Now, this is way easier to understand:
at the end, j will be zero, as it's the exit condition of the loop;
the loop will run twice, as the third time ++j is evaluated it will evaluate to zero;
y will end as 4, as it starts at zero, the loop runs twice and each time it's incremented by 2;
k is never touched, so it stays at -3;
in the loop body, k<j always, as they start equal but j has already been incremented once the control flow reach x+=k<j, so this statement becomes ++x. As it runs twice, x becomes -1.
Im trying to learn C, surely using the hard way and cant figure out this one error, could someone help? :-)
#include<stdio.h>
#include <stdlib.h>
#define max_X 15
#define max_Y 15
int x, y;
char Array[max_Y][max_X];
void displayArray(void){
for (y = 0; y < max_Y; y++) {
for (x = 0; x < max_X; x++) {
printf("%c",Array[y][x]);
}
printf("\n");
}
}
int main(void){
for (y = 0; y < max_Y; y++) {
for (x = 0; x < max_X; x++) {
Array[y][x] = '.';
}
}
displayArray;
getchar;
return(0);
}
Im trying to print out char array containing just dot characters using function. When i run it, there is just blank cmd and return value 0. I keep getting warnings about statements with no effect on these two lines:
displayArray;
getchar;
Can someone help? or give me a link to similar one where i can find answer to my problem? I was looking around but couldn't find anything i could compare to mine and understand at least a little.
You need to use parentheses even when the function you're using takes no arguments. So,
displayArray;
getchar;
should be:
displayArray();
getchar();
Also, return isn't a function. It's a keyword, so you can do:
return 0;
Use displayArray();
You should not call function like this displayArray;you can use this while providing address of function to function pointer.
I'm a beginner at C and can't find the answer to this anywhere because I don't know how to word it so I'm just going to ask a question.
I understand basic looping but what I want to do is make a small program where the output is this using only loops:
12345
1234
123
12
1
my code so far
int x;
int y = 1;
for (x=1; x<=5; x++)
{
while(y<=5)
{
printf("%d", y);
y++;
}
}
this is what I have so far and don't know where to go next, if anyone could help I'd be grateful.
Close. Your outer loop needs to count down, not up, and your inner loop needs to count from 1 to x.
int x, y;
for (x=5; x>=1; x--)
{
for (y=1;y<=x;y++)
{
printf("%d", y);
}
printf("\n");
}
This is another solution using just a FOR loop with Integer Division. Try:
#include <stdio.h>
int main(void){
int n= 12345, i;
for(i=n;i>0;i/=10){
printf("%d\n",i);
}
return 0;
}
You have to see the pattern you want and apply it in the loops to get it.
You need the output 12345 1234 123 12 1. So, the first iteration should start at 1 and go till 5, second should start at 1 and go till 4, and so on..
So, the outer loop should give the end limits for the inner loop, and the inner one should always start with 1.
Try
for (x=5; x>=1; x--)
{
y = 1; // because the number always start with 1
while(y<=x)
{
printf("%d", y);
y++;
}
printf("\n"); //to go to next line
}
For fun: another approach that simply divides each loop. But use best answer
int main(void) {
int x = 12345;
do {
printf("%d\n", x);
x /= 10;
} while (x);
return 0;
}
Output
12345
1234
123
12
1
Another solution is to use integer division and use one single loop:
int x = 12345;
// repeat this loop as long as x != 0
while (x)
{
printf("%d\n", x);
x /= 10; // x = x/10;
}
You need to make 2 changes to your outer loop in order to get the expected output.
for (x=1; x<=5; x++) should be for (x=1; x<=5; x--) because your program will count up to 5 the first time, 4 the second time, 3 the third time, etc..
You should include printf("\n") so a new line is printed after each sequence is output.
You need to make 1 change to your inner loop as well:
Instead of y <= 5 you should say y <= x because x will always be equal to the last number that you want to print since you're decrementing it in the outer loop.
Is there a O(n) way to draw a 2-D array grid instead of O(n²) in C? For instance, I'm wondering if there's a way to use one for loop instead of two. I always felt like there was a way, but thought I'd turn it over to you...
ARRAY2D grid;
void ShowGrid(void)
{
int x, y;
for (y = 0; y < grid.height; y++)
{
for (x = 0; x < grid.width; x++)
{
printf("%d", arr2_get(&grid, x, y));
}
printf("\n");
}
printf("\n");
}
You normally study what happens as you increase the number of elements.
If that's the case here, the code you posted is O(N), not O(N2).
If the grid has 100 elements, the number of times printf will be called will be proportional to 100 (O(N)), not proportional to 10,000 (O(N2)).
If on the other handle you were studying what happens as you increase the number of rows or columns, visiting all the elements will be at least O(R*C) since you have R*C elements.
Note that you can flatten the two loops, but it doesn't change the complexity in the least:
void ShowGrid()
{
const int H = grid.height;
const int W = grid.width;
int n;
for (n = 0; n < H*W; ++n)
{
int y = n / W;
int x = n % W;
printf("%d", arr2_get(&grid, x, y));
printf("\n") if x == W-1;
}
}
The following code uses a single for loop:
void ShowGrid(void)
{
int i;
for (i = 0; i < grid.width * grid.height; i++)
{
int x = i % grid.width;
int y = i / grid.width;
printf("%d", arr2_get(&grid, x, y));
if (x == grid.width - 1)
printf("\n");
}
printf("\n");
}
Another approach without multiplication or division:
void ShowGrid(void)
{
int x = 0;
int y = 0;
while (y < grid.height) {
printf("%d", arr2_get(&grid, x, y));
x += 1;
if (x == grid.width) {
printf("\n");
x = 0;
y += 1;
}
}
printf("\n");
}
But the time complexity is obviously the same as with two for loops.
There are ways to structure your 2d matrix as a 1 dimensional array or your loop variables, which will allow you to use 1 loop. However, this will not reduce the run time complexity at all.
In your situation, you are always going to have to access each cell of the 2d grid.
If you define N = Width*Height. Your code is already O(n). If you define N = Width = Height, then your code is O(N^2). Neither of these can be improved.