[SOLVED] C question about updating a variable for a placeholder
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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 );
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...
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.