By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,668 Members | 1,477 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,668 IT Pros & Developers. It's quick & easy.

Printing a table of numbers power to 2

P: 1
I need to make a table of numbers starting from 3, index going from 0 to 29 so that every number gets multiplied by two. So the table should look like:

3 6 12 24 48 96 ....

This is what I got and it doesn't work at all..

void show_table(long int *a, size_t n)
int i;
for (i = 0; i < n; ++i) {
printf("%ld ", a[i]);
*a = *a *2;
int main(void)
long table[] = {3};
show_table(table, 29);
Jan 31 '19 #1
Share this Question
Share on Google+
1 Reply

Expert 100+
P: 2,400
1. Add some comments expaining what software is supposed to do.

2. Definition of table array in main: dimension of array not specified so compiler infers array size from initializer, only one initializer value so array-of-1 is defined.

3. Suggest you separate array initialization from array printing in show_table. Loop once for initialization; after that loop again for printing. Even better to have two subroutines: one to initialize and one to print. Call them sequentially from main.

4. Assignment instruction in show_table sets each element to twice what it was. You want to set each element to twice the value of the previous element. Be careful how you handle the first and last passes through the loop ó donít access a[-1] or a[n].

Wrap your code snippet in CODE tags so (a) spacing and indentation are preserved, and (b) lines are numbered.
Feb 1 '19 #2

Post your reply

Sign in to post your reply or Sign up for a free account.