Basic C formatting issue

For this assignment I want my program to output the requested output. My issue lies in the formatting. Link to the problem in question:

The output of the program:

For whatever reason my x value is not increasing by a factor of ten and as far as the spacing is concerned I am not sure what I am doing wrong.

#include <stdio.h>

//declare global variables
    int x = 1;
    double num = .1234;

//prototype functions
partA(double num);

int main() {
    double a;
    a = partA(num);
    printf("%lf\n",a);
}

/*First Function, x and num increase by a factor
of ten. */ 
partA(double num) {
    for(x; x <= 10000; x *= 10) {
        for (num; num <= 1234; num *= 10) {
            printf("%d%4lf\n",x, num);
        }
        printf("\n");
    }   
}

Answers


After accepted answer. This one uses fields widths and not spaces as required by the post.

#include <stdio.h>

void partAB(const char *format, double num) {
  double d = 1.0;
  for (int i = 0; i<5; i++) {
     printf(format, d, num);
     d *= 10;
     num *= 10;
   }
   printf("\n");
}

void partA(double num) {
  partAB("%5.0lf  %.4lf\n", num);
  }

void partB(double num) {
  partAB("%5.0lf  %9.3le\n", num);
  }

int main() {
  double x = 0.1234;
  partA(x);
  partB(x);
  return 0;
}

The spacing is because only outer loop is executing which contains printf


this code:

#include <stdio.h>

int ee(int e){
  int i, ret;
  ret=1;
  for(i=0;i<e;i++)
    ret *= 10;
  return ret;
}

int main(void) {
  int i;
  printf("a)\n");
  for(i=0;i<5;i++)
    printf("%5d %.4f\n",ee(i), 0.1234 * ee(i));
  printf("\nb)\n");
  for(i=0;i<5;i++)
    printf("%5d %.3e\n",ee(i), 0.1234 * ee(i));
}

gives:

a)
    1 0.1234
   10 1.2340
  100 12.3400
 1000 123.4000
10000 1234.0000

b)
    1 1.234e-01
   10 1.234e+00
  100 1.234e+01
 1000 1.234e+02
10000 1.234e+03

It is not increasing because you have a For inside another For, you have to increment X inside the For of "num"

Something like:

int x = 1;
for (num; num <= 1234; num *= 10)
{
    printf("%5d%3s%4lf\n",x, " ", num);
    x*=10;
}

Cheers

Edit: before I forgot the spaces, it should be fine now


Without if, switch and spaces and tabs in printf format it will be like this

partA(double num) {
    const int const max = 10000;
    const int const precision = 4;
    int width = 1 + log((float)max) / log(10.0f);
    for(; x <= max; x *= 10, num *= 10) {
        printf("%*d%c%.*f\n", width, x, 040, precision, num);
    }
}

See the documentation for printf format strings.

To change the number of digits after the decimal point, specify the precision.

To get a space between the numbers, use a literal space in the format string, as in

printf("%d %4lf\n",x, num);

Need Your Help

update query with inner join access vba code

vba ms-access access-vba inner-join

I am trying to update a table from another one via a inner join query access vba. The field's name to be updated will be taken in a form.

How to handle "org.openqa.selenium.WebDriverException: ChromeDriver only supports characters in the BMP" exception?

java google-chrome selenium

Whenever I try to fill some non BMP character like '🏀' as input to a text area using selenium, my chrome driver fails due to following exception: