Jump to content

Help with C.

Go to solution Solved by Art Vandelay,

I'm tryna make sense out of this code but I can't seem to fully understand it...can you please explain this further? just the printf bit at least... 

I think it works out to:

		// % is the modulo operator		if ((x+1)%7 != 0) //(x+1)%7?"\t%d":"\t%d\n" this is a ternary operator, basically if-else on the same line. 0 means false, not 0 means true in a boolean expression, you don't need a comparison.		{			if ((x/7) % 2 == 1) //x/7&1?6-x%7:x%7 another ternary operator. & is the logical AND operation. doing &1 checks the last binary digit, which is 1 if odd, 0 if even.			{				printf("\t%d",(arr)[x/7*7 + 6-x%7]); //x/7 is the row at which the point is. Adding to the variable arr is the same as adding the same number to the index								     //this is because arr is the memory index of the first element of the array, and adding 1 to arr iterates                                                                      //the memory index by 1, pointing to the next index		                                                     //note that iterating the memory pointer by 1 moves you 1*(the size of the data type) in memory			}			else 			{				printf("\t%d",(arr)[x/7*7 + x%7]);			}		}		else		{			if ((x/7) % 2 == 1)			{				printf("\t%d\n",(arr)[x/7*7 + 6-x%7]);			}			else 			{				printf("\t%d\n",(arr)[x/7*7 + x%7]);			}		}	}

Hopefully that helps, and please never use modulo ternary operators in your code. They are very irritating to read. 

 

edit: I said modulo instead of ternary by accident. I don't really proof read.

So I'm insanely new and fresh to coding with little to no experience at all.
 
can someone help me with C?

 

here's the code so far:

#include<stdio.h>#include<conio.h>main()	{	 int x,arr[7][9]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63};	 clrscr();	 for(x=0;x<63;x++);	  {	   printf("\t%d \t%d \t%d \t%d \t%d \t%d \t%d\n",arr[0][0],arr[0][1],arr[0][2],arr[0][3],arr[0][4],arr[0][5],arr[0][6]);	   printf("\t%d \t%d \t%d \t%d \t%d \t%d \t%d\n",arr[0][13],arr[0][12],arr[0][11],arr[0][10],arr[0][9],arr[0][8],arr[0][7]);	   printf("\t%d \t%d \t%d \t%d \t%d \t%d \t%d\n",arr[0][14],arr[0][15],arr[0][16],arr[0][17],arr[0][18],arr[0][19],arr[0][20]);	   printf("\t%d \t%d \t%d \t%d \t%d \t%d \t%d\n",arr[0][27],arr[0][26],arr[0][25],arr[0][24],arr[0][23],arr[0][22],arr[0][21]);	   printf("\t%d \t%d \t%d \t%d \t%d \t%d \t%d\n",arr[0][28],arr[0][29],arr[0][30],arr[0][31],arr[0][32],arr[0][33],arr[0][34]);	   printf("\t%d \t%d \t%d \t%d \t%d \t%d \t%d\n",arr[0][41],arr[0][40],arr[0][39],arr[0][38],arr[0][37],arr[0][36],arr[0][35]);	   printf("\t%d \t%d \t%d \t%d \t%d \t%d \t%d\n",arr[0][42],arr[0][43],arr[0][44],arr[0][45],arr[0][46],arr[0][47],arr[0][48]);	   printf("\t%d \t%d \t%d \t%d \t%d \t%d \t%d\n",arr[0][55],arr[0][54],arr[0][53],arr[0][52],arr[0][51],arr[0][50],arr[0][49]);	   printf("\t%d \t%d \t%d \t%d \t%d \t%d \t%d\n",arr[0][56],arr[0][57],arr[0][58],arr[0][59],arr[0][60],arr[0][61],arr[0][62]);	  }	 getch();	 return 0;	}

Output:

post-61090-0-09031500-1415903440.png

 

I do not completely understand how this code works or fully understand arrays at this point. I get the general idea of arrays, but I just don't know how to properly and effectively code it yet. 

 

and I'm also wondering how to get the same output using some for loop statements. and less lines of code. 

 

 

any help would be greatly appreciated! Thanks! 

 

p.s. links and other stuff that'll teach me about C would be appreciated as well! 

Potatoes are the bacon of vegetables... nuff' said

MOBO: Asrock H67DE3, CPU: Intel Core i5 2400 @3.1GHz, GPU: Asus GTS 450 DirectCU Silent, Ram: 16gb 1333MHz, HDD: WD 1TB Green, PSU: 500 watt

Link to comment
https://linustechtips.com/topic/250043-help-with-c/
Share on other sites

Link to post
Share on other sites

instead of that for should be

int i, j;int n = 7;int m = 9;int k = 0;for (i < 0; i < n; i++){       if (k == 0)         for (j = 0; j < m; j++)         {              printf(" %i", arr[i][j];              k = 1;         }       else         for (j = m-1; j > 0; j--)         {              printf(" %i", arr[i][j];              k = 0;         }       printf("/n");}

and when giving values to matrix you should add new line between each row.

 

and for tutorials go to https://buckysroom.org/index.php

Link to comment
https://linustechtips.com/topic/250043-help-with-c/#findComment-3428493
Share on other sites

Link to post
Share on other sites

I know this doesn't answer the question you asked, but if you're this new to programming then you definitely don't want to start with C. Start with a high level language like Java/C# which will introduce you to the concepts and design patterns for programming then move onto a lower level language like C to get a better feel for the implementation of languages e.g. pointers and memory management.

Link to comment
https://linustechtips.com/topic/250043-help-with-c/#findComment-3428524
Share on other sites

Link to post
Share on other sites

instead of that for should be

int i, j;int n = 7;int m = 9;for (i < 0; i < n; i++){       for (j = 0; j < m; j++)       {             printf(" %i", arr[i][j];       }       printf("/n");}

and when giving values to matrix you should add new line between each row.

 

and for tutorials go to https://buckysroom.org/index.php

 

Maybe there's something I'm missing (or I'm just stupid ^^), but doesn't that print them in sequential order left to right? In the op the output was with the first line left to right, the second rigth to left, the third left to right again etc.

 

I think there should be an 'if' statement that checks if the value of i is even or odd (and print the values accordingly).

 

but again I'm tired and what I just wrote might make no sense... :P

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
https://linustechtips.com/topic/250043-help-with-c/#findComment-3428648
Share on other sites

Link to post
Share on other sites

Maybe there's something I'm missing (or I'm just stupid ^^), but doesn't that print them in sequential order left to right? In the op the output was with the first line left to right, the second rigth to left, the third left to right again etc.

 

I think there should be an 'if' statement that checks if the value of i is even or odd (and print the values accordingly).

 

but again I'm tired and what I just wrote might make no sense... :P

lol just noticed that.

I will edit it :)

Link to comment
https://linustechtips.com/topic/250043-help-with-c/#findComment-3428662
Share on other sites

Link to post
Share on other sites

I have feeling that OP is not that good at programming to use recursions.

not good enough yet at least ;)

Potatoes are the bacon of vegetables... nuff' said

MOBO: Asrock H67DE3, CPU: Intel Core i5 2400 @3.1GHz, GPU: Asus GTS 450 DirectCU Silent, Ram: 16gb 1333MHz, HDD: WD 1TB Green, PSU: 500 watt

Link to comment
https://linustechtips.com/topic/250043-help-with-c/#findComment-3430010
Share on other sites

Link to post
Share on other sites

instead of that for should be

int i, j;int n = 7;int m = 9;int k = 0;for (i < 0; i < n; i++){       if (k == 0)         for (j = 0; j < m; j++)         {              printf(" %i", arr[i][j];              k = 1;         }       else         for (j = m-1; j > 0; j--)         {              printf(" %i", arr[i][j];              k = 0;         }       printf("/n");}
and when giving values to matrix you should add new line between each row.

and for tutorials go to https://buckysroom.org/index.php

I'll try this one!! thanks!!

Potatoes are the bacon of vegetables... nuff' said

MOBO: Asrock H67DE3, CPU: Intel Core i5 2400 @3.1GHz, GPU: Asus GTS 450 DirectCU Silent, Ram: 16gb 1333MHz, HDD: WD 1TB Green, PSU: 500 watt

Link to comment
https://linustechtips.com/topic/250043-help-with-c/#findComment-3430012
Share on other sites

Link to post
Share on other sites

Use a recursive function to make it efficient. All good algorithms have the ability to be implemented recursively.

 

Recursive algorithms are not always the most efficient method and just because something can be implemented using recursion does not always mean it should be.

Link to comment
https://linustechtips.com/topic/250043-help-with-c/#findComment-3430222
Share on other sites

Link to post
Share on other sites

Recursive algorithms are not always the most efficient method and just because something can be implemented using recursion does not always mean it should be.

 

Based on the scenario of the OP and Big-Oh notation, having two for-loops causes a O(n^2) worst case scenario while having a recursive algorithm would make it more linear where the algorithm would be O(n). So O(n) would be better than O(n^2). But yes, in some cases it is the other way around as you have stated.

Link to comment
https://linustechtips.com/topic/250043-help-with-c/#findComment-3430785
Share on other sites

Link to post
Share on other sites

Based on the scenario of the OP and Big-Oh notation, having two for-loops causes a O(n^2) worst case scenario while having a recursive algorithm would make it more linear where the algorithm would be O(n). So O(n) would be better than O(n^2). But yes, in some cases it is the other way around as you have stated.

 

Nested for-loops is a form of recursion called tail recursion. I'd like to see your O(n) recursive algorithm for this because I'm not sure one exists.

Link to comment
https://linustechtips.com/topic/250043-help-with-c/#findComment-3430816
Share on other sites

Link to post
Share on other sites

intmain(void){    int arr[63] =   {                        1,2,3,4,5,6,7,                        8,9,10,11,12,13,14,                        15,16,17,18,19,20,21,                        22,23,24,25,26,27,28,                        29,30,31,32,33,34,35,                        36,37,38,39,40,41,42,                        43,44,45,46,47,48,49,                        50,51,52,53,54,55,56,                        57,58,59,60,61,62,63                    } ;    int x = 0;    for(; x < 63; x++)        printf((x+1)%7?"\t%d":"\t%d\n",(arr+x/7*7)[x/7&1?6-x%7:x%7]);    return 0;}

I'd buy a book. http://www.amazon.com/Programming-3rd-Stephen-G-Kochan/dp/0672326663/ref=sr_1_8?s=books&ie=UTF8&qid=1415929027&sr=1-8&keywords=c+programming

main(i){for(;i<101;i++)printf("Fizz\n\0Fizzz\bBuzz\n\0%d\n"+(!(i%5)^!!(i%3)*3)*6,i);}

Link to comment
https://linustechtips.com/topic/250043-help-with-c/#findComment-3431107
Share on other sites

Link to post
Share on other sites

Rig: i7 2600K @ 4.2GHz, Larkooler Watercooling System, MSI Z68a-gd80-G3, 8GB G.Skill Sniper 1600MHz CL9, Gigabyte GTX 670 Windforce 3x 2GB OC, Samsung 840 250GB, 1TB WD Caviar Blue, Auzentech X-FI Forte 7.1, XFX PRO650W, Silverstone RV02 Monitors: Asus PB278Q, LG W2243S-PF (Gaming / overclocked to 74Hz) Peripherals: Logitech G9x Laser, QPad MK-50, AudioTechnica ATH AD700

Link to comment
https://linustechtips.com/topic/250043-help-with-c/#findComment-3431488
Share on other sites

Link to post
Share on other sites

thank you guys so much I'll be sure to check all of these out!!

Potatoes are the bacon of vegetables... nuff' said

MOBO: Asrock H67DE3, CPU: Intel Core i5 2400 @3.1GHz, GPU: Asus GTS 450 DirectCU Silent, Ram: 16gb 1333MHz, HDD: WD 1TB Green, PSU: 500 watt

Link to comment
https://linustechtips.com/topic/250043-help-with-c/#findComment-3432094
Share on other sites

Link to post
Share on other sites

I know this doesn't answer the question you asked, but if you're this new to programming then you definitely don't want to start with C. Start with a high level language like Java/C# which will introduce you to the concepts and design patterns for programming then move onto a lower level language like C to get a better feel for the implementation of languages e.g. pointers and memory management.

I'll never understand why people keep saying this. When you're starting, it's all the same. You're not going to be calling malloc or needing to create a complex structure, you're just going to go do some basic things with compiler managed primitive variables. The only downside to C in that respect is that windows will terminate the program when you try to access an array out of its bounds, instead of it telling you that you tried to do that, but once you know that that's the reason it's pretty much the same to debug.

Java/C# introduces a whole extra level of complexity with classes and API calls. I never found classes easy to understand when I started and the API calls in Java/C# still confuse me sometimes, years later.

 

 

Use a recursive function to make it efficient. All good algorithms have the ability to be implemented recursively.

What? How would recursion make it faster? Recursion is almost always slower and more uses more memory, in C, than iteration.

 

Generally recursion is used in C because making some algorithms iteratively is horrific to do. Navigating a non binary tree iteratively hurts.

Link to comment
https://linustechtips.com/topic/250043-help-with-c/#findComment-3440788
Share on other sites

Link to post
Share on other sites

intmain(void){    int arr[63] =   {                        1,2,3,4,5,6,7,                        8,9,10,11,12,13,14,                        15,16,17,18,19,20,21,                        22,23,24,25,26,27,28,                        29,30,31,32,33,34,35,                        36,37,38,39,40,41,42,                        43,44,45,46,47,48,49,                        50,51,52,53,54,55,56,                        57,58,59,60,61,62,63                    } ;    int x = 0;    for(; x < 63; x++)        printf((x+1)%7?"\t%d":"\t%d\n",(arr+x/7*7)[x/7&1?6-x%7:x%7]);    return 0;}

I'd buy a book. http://www.amazon.com/Programming-3rd-Stephen-G-Kochan/dp/0672326663/ref=sr_1_8?s=books&ie=UTF8&qid=1415929027&sr=1-8&keywords=c+programming

 

I'm tryna make sense out of this code but I can't seem to fully understand it...can you please explain this further? just the printf bit at least... 

Potatoes are the bacon of vegetables... nuff' said

MOBO: Asrock H67DE3, CPU: Intel Core i5 2400 @3.1GHz, GPU: Asus GTS 450 DirectCU Silent, Ram: 16gb 1333MHz, HDD: WD 1TB Green, PSU: 500 watt

Link to comment
https://linustechtips.com/topic/250043-help-with-c/#findComment-3448989
Share on other sites

Link to post
Share on other sites

I'm tryna make sense out of this code but I can't seem to fully understand it...can you please explain this further? just the printf bit at least... 

Unless you're doing something like code golf, don't write code like that. Write nice code that is, at least, reasonably easy to read and understand lol

Link to comment
https://linustechtips.com/topic/250043-help-with-c/#findComment-3450662
Share on other sites

Link to post
Share on other sites

I'm tryna make sense out of this code but I can't seem to fully understand it...can you please explain this further? just the printf bit at least... 

I think it works out to:

		// % is the modulo operator		if ((x+1)%7 != 0) //(x+1)%7?"\t%d":"\t%d\n" this is a ternary operator, basically if-else on the same line. 0 means false, not 0 means true in a boolean expression, you don't need a comparison.		{			if ((x/7) % 2 == 1) //x/7&1?6-x%7:x%7 another ternary operator. & is the logical AND operation. doing &1 checks the last binary digit, which is 1 if odd, 0 if even.			{				printf("\t%d",(arr)[x/7*7 + 6-x%7]); //x/7 is the row at which the point is. Adding to the variable arr is the same as adding the same number to the index								     //this is because arr is the memory index of the first element of the array, and adding 1 to arr iterates                                                                      //the memory index by 1, pointing to the next index		                                                     //note that iterating the memory pointer by 1 moves you 1*(the size of the data type) in memory			}			else 			{				printf("\t%d",(arr)[x/7*7 + x%7]);			}		}		else		{			if ((x/7) % 2 == 1)			{				printf("\t%d\n",(arr)[x/7*7 + 6-x%7]);			}			else 			{				printf("\t%d\n",(arr)[x/7*7 + x%7]);			}		}	}

Hopefully that helps, and please never use modulo ternary operators in your code. They are very irritating to read. 

 

edit: I said modulo instead of ternary by accident. I don't really proof read.

Link to comment
https://linustechtips.com/topic/250043-help-with-c/#findComment-3451533
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×