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
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 )
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
#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..
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
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)));
}
}
}
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.