sqrt table + command line argument

I need to create a program that provides a table of numbers and their square roots using a command line argument. I have the command line argument down, im just unsure how to do the square root table

closed account (E3h7X9L8)
use setw() stream manipulator to set widgth between numbers and their square , setw is a parameterized stream manipulator and you must include <iomanip> header

here is an example so you can understand how it works:

std::cout << 5 << std::setw(15) << 5*5 ;

this would display:

5--------------25

(x13 '-' which are white spaces, i cant represent them in this post, two of them are "eaten" by '25' because the numbers or characters are written from right to left in output )

Last edited on
Say i was to type something like a.out -15 30 5
I need to produce a table of numbers and their square roots which begins at -15 and goes in steps of 5 up to 30. I'm still quite new and I don't really know what you mean by your statement. I always see people on this forum type cout, but my teacher tells us to use printf. I don't know how to implement the numbers from the command line such as -15, 30, 5 into a for loop or whatever i need to do
Are you writing in C or C++? If you are writing C, you use printf(). In C++, we use std::cout.

Could you post what you have so we can give tips about where to find what you're looking for?
closed account (E3h7X9L8)
i dont really know how command line argument works .but im still gonna give you the loop



for(int i = -15; i <= 30; i += 5)
    printf("Number and square: %10d %10d \n", i, i*i);



just replace -15 30 5 with the variables you input
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#include <math.h>

int main(int argc, char *argv[4]){
   int beg, end, step, i;
   if (argc==4) {
      sscanf(argv[1], "%d", &beg);
      sscanf(argv[2], "%d", &end);
      sscanf(argv[3], "%d", &step);
   }
   if (argc!=4) {
      printf("Error, What is your starting number?\n");
      scanf("%d", &beg);
      printf("What is your number to stop at?\n");
      scanf("%d", &end);
      printf("What is your step size?\n");
      scanf("%d", &step);
   }
   for (i = beg; i = end; i +=5) {
      printf("%d   %.2f\n", i, i*i); 
   }
}

Kind of implemented what leryss just said but yeah..
He said square roots, not squares, so:

printf("%d %.2f\n", i, sqrt(i*1.0));
Also:
for (i = beg; i <= end; i +=step)
Last edited on
alright, so im getting nan error which is "not a number" apparently. for exmaple sqrt of -25 would just be 5i. how would i go about doing that
You could try using the complex number functions/types the library provides, but it is going to be more complex than sticking to the reals.
http://en.cppreference.com/w/c/numeric/complex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
   if ((beg < end) && (end > 0)) {
      for (i = beg; i <= end; i +=step) {
           if (i > 0)
              printf("%d   %.2f\n", i, sqrt(i));
           if (i < 0)
              printf("%d   %.2fi\n", i, sqrt(abs(i)));
           }
   }
   if ((beg > end) && (end < 0)) {
      for (i = beg; i >= end; i += step) {
           if (i > 0)
              printf("%d   %.2f\n", i, sqrt(i));
           if (i < 0)
              printf("%d   %.2fi\n", i, sqrt(abs(i)));
           }
   }
}


-15   3.87i
-10   3.16i
-5   2.24i
5   2.24
10   3.16
15   3.87
20   4.47
25   5.00
30   5.48

how would i go about outputting the numbers on the right to align up
Place the width you want the number to take up after the percent sign but before the decimal point in your format string; that is a width that it will try to align to. See what happens.
Topic archived. No new replies allowed.