LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-18-2013, 12:22 PM   #1
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
C question about updating a variable for a placeholder


in the printf (" foo %d", d);

is there a way to use a for loop to update a space for the place holder. so if i wanted %4d as the start, and then add 2 every time until i get to 10...

%4d first time
%6d 2nd
%8d etc... until this has been done 10 times.

I am trying to create a multiplication table with the following layout.

for my first row i just used printf to manually spread out the top row of numbers:

Code:
printf("    1  2  3  4  5  6  7  8  9  10");	// print out of top row
so nothing fancy there. for the multiplication im just using a simple for loop.

Code:
for (i = 1; i <= 10; i+=1);
ill use that to update the end of the printf statement with a*i. this is where my question comes in about the while loop for updating the # for the place holder.

Thanks. If it can be done please point me in the right direction to accomplish this task. examples are good, exact code is NOT what i am after as this is homework. Please do not do my homework for me, but help and guidance is greatly appreciated.
 
Old 07-18-2013, 12:50 PM   #2
jmwebb2112
LQ Newbie
 
Registered: Jul 2013
Posts: 1

Rep: Reputation: Disabled
You should be able to use the syntax printf( "%*d", number, int ), where you supply the value of the number of spaces in the parameter that corresponds with the placement of the asterisk. For example:

int spaces = 5
int value = 10;
printf( "%*d", spaces, value );
 
Old 07-18-2013, 12:55 PM   #3
thirdm
Member
 
Registered: May 2013
Location: Massachusetts
Distribution: Slackware, NetBSD, Debian, 9front
Posts: 316

Rep: Reputation: Disabled
One of us is slightly confused I think. When you say this...
Quote:
%4d first time
%6d 2nd
%8d etc... until this has been done 10 times.
... what do you mean by first time, 2nd time, etc.? Are you thinking you're calling printf 10 times in the loop each time with a larger width in the format string? If you think about it, or do it on paper, that's not right. If you did it that way, then the first output's right side would be at 4, 2nd six away from there, third one eight away from that new end -- won't line up at all. Remember that once you've called printf, the output stream has advanced to the end of what you output. It almost sounds like, if I'm understanding you, that you're expecting some kind of overlay output where it goes back to the beginning of the line with each call, like you're on a typewriter and doing a carriage return with no linefeed.

Now, you could build the format string in a loop (a format string doesn't have to be a string literal) and then perhaps call printf or another function in that family once with all the arguments accumulated into somekind of stdarg variable arg thingy, but that would be crazy. Rather there isn't a need for different format spec widths here at all other than perhaps the differences in widths between 9 and 10. Each call to printf with each number in your loop should have the same width.

As an aside, don't start loop indexes from one. It's the C idiom to count from zero (as all sane languages do). Also, it's more idiomatic to increase the loop variable with the ++ operator rather than +=. So an idiomatic loop is written like this...

for (i = 0; i < 10; i++)
...

or maybe (particularly for C++ programmers)

for (i = 0; i != 10; ++i)
...
 
Old 07-18-2013, 01:51 PM   #4
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
thank you both. im sorry i forgot to give an example of the desired display, this might clear up some of the confusion
Code:
    1  2  3  4
1   1  2  3  4
2   2  4  6  8
3   3  6  9  12
4   4  8  12 16
I need to do that from 1 - 10 in both row and column.

the first row is displayed via printf, i was thinking for the following 10 row to start with the variable, then use that variable in a counting type loop to multiply my for loop with the i above.

am i at least on the right track?

the only reason i didnt start at 0, was 0 * foo is always 0, so i was going to skip that row entirely. no need to waste CPU time, or display for a 0 row.

Last edited by lleb; 07-18-2013 at 01:56 PM.
 
Old 07-18-2013, 02:30 PM   #5
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
never mind, i was trying to make it much more complicated then i needed. i was able to embed a loop inside of a loop.

Code:
#include<stdlib.h>
#include<stdio.h>

// create the top to skip a number holder slot, then print 1 - 10 in a line with spaces
// create the side to skip a line, then print 1 - 10 down no spaces between lines
// align row/colum with sum of value of the top/side spaced properly

int main(void)
{

	int rows, columns;	// input variables as integers to make table easier to read and calculate

	// for loop with initial condition = 1, test will end at 10, and update is increase by 1 this should go across the top
	for ( rows = 1; rows <=10; rows+=1)
	{
		// for loop with initial condition = 1, test will end at 10, and update is increase by 1 this should go down.  This loop is embeded into the first loop.
		for ( columns = 1; columns <= 10; columns+=1)
		{
		// this should take the sum of the column and the row and spread them out by tabs \t
		printf("%d\t", columns * rows);
		}
	// to make it easier to read, add a space between lines
	printf("\n");
	}
}
this way each time the inner loop runs then it updates the outer loop to include the \n. this works as desired.

Code:
imac:ENG-3211 user$ gcc -o hw_4_2 hw_4_2.c
imac:ENG-3211 user$ ./hw_4_2 
1	2	3	4	5	6	7	8	9	10	
2	4	6	8	10	12	14	16	18	20	
3	6	9	12	15	18	21	24	27	30	
4	8	12	16	20	24	28	32	36	40	
5	10	15	20	25	30	35	40	45	50	
6	12	18	24	30	36	42	48	54	60	
7	14	21	28	35	42	49	56	63	70	
8	16	24	32	40	48	56	64	72	80	
9	18	27	36	45	54	63	72	81	90	
10	20	30	40	50	60	70	80	90	100
thanks for the help earlier. they got me looking for a much better solution and my head onto the right track.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
.placeholder files sulekha Ubuntu 2 09-08-2008 11:48 PM
Just white space in video placeholder..!! Hitboxx General 3 03-27-2007 11:13 AM
java-placeholder error mzizana Linux - Software 0 11-15-2006 01:17 AM
Changing placeholder eliw42 Linux - Networking 0 08-22-2006 02:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration