Help with arithmetic project.

Pages: 12
Task 1 : display a table for y = -3x + 2 from – 5 to 5.

Task 2: Display the square and cube of x = 1 to 20

The output has to be displayed in a table form. I would display my code but it's absolutely wrong.
Task 1: The table needs only two columns: x and y.

 x    y 
---  ---
 -5   17
 -4   14
 -3   11
 -2    8
 -1    5
  0    2
  1   -1
  2   -4
  3   -7
  4  -10
  5  -13

So, you need a loop for the (input) values of x.
You need an equation to get the (output) values of y.

Task 2 is the same thing, except you have changed the table to list two computed quantities.

 x    y1   y2
---  ---  ---
  1    1    1
  2    4    8
...
 20  400  8000

Hint: You do not actually need to use arrays or anything like that. Before your loop, print your table header. Then create your loop for the values of x. Inside the loop, print x, then compute and print each required value. Don’t forget to print a newline between your two tables.

Good luck!
Thank you I finished it but without using loops as my instructor specifically said no loops so I used setw. I would share here but its too long.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <cstdio>

template<typename T>
void display( T xmin, T xmax, int n, const char *fmt, T f( T), T g( T ) = nullptr )
{
   T dx = ( xmax - xmin ) / ( n - 1 );
   for ( int i = 0; i < n; i++ ) 
   {
      T x = xmin + i * dx;
      if ( g ) printf( fmt, x, f(x), g(x) );
      else     printf( fmt, x, f(x) );
   }
}


int main()
{
   printf( "%6s\t%6s\n", "x", "-3x+2" );
   display<double>( -5, 5, 11, "%6.1f\t%6.1f\n", []( double x ){ return -3 * x + 2; } );

   printf( "\n%6s\t%6s\t%6s\n", "x", "x^2", "x^3" );
   display<int>( 0, 20, 21, "%6d\t%6d\t%6d\n", []( int x ){ return x * x; }, []( int x ){ return x * x * x; } );
}


     x	 -3x+2
  -5.0	  17.0
  -4.0	  14.0
  -3.0	  11.0
  -2.0	   8.0
  -1.0	   5.0
   0.0	   2.0
   1.0	  -1.0
   2.0	  -4.0
   3.0	  -7.0
   4.0	 -10.0
   5.0	 -13.0

     x	   x^2	   x^3
     0	     0	     0
     1	     1	     1
     2	     4	     8
     3	     9	    27
     4	    16	    64
     5	    25	   125
     6	    36	   216
     7	    49	   343
     8	    64	   512
     9	    81	   729
    10	   100	  1000
    11	   121	  1331
    12	   144	  1728
    13	   169	  2197
    14	   196	  2744
    15	   225	  3375
    16	   256	  4096
    17	   289	  4913
    18	   324	  5832
    19	   361	  6859
    20	   400	  8000

Lookie lookie, no loops!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cstdio>

template<typename T>
void display( T xmin, T xmax, int i, int n, const char *fmt, T f( T), T g( T ) = nullptr )
{
   T dx = ( xmax - xmin ) / ( n - 1 );
   if (i < n)
   {
      T x = xmin + i * dx;
      if ( g ) printf( fmt, x, f(x), g(x) );
      else     printf( fmt, x, f(x) );
      display<T>( xmin, xmax, i+1, n, fmt, f, g );
   }
}


int main()
{
   printf( "%6s\t%6s\n", "x", "-3x+2" );
   display<double>( -5, 5, 0, 11, "%6.1f\t%6.1f\n", []( double x ){ return -3 * x + 2; } );

   printf( "\n%6s\t%6s\t%6s\n", "x", "x^2", "x^3" );
   display<int>( 0, 20, 0, 21, "%6d\t%6d\t%6d\n", []( int x ){ return x * x; }, []( int x ){ return x * x * x; } );
}

I still cannot fathom instructors who give beginners homeworks that say “compute a table of stuff using a function but no loops, sucka's”.
#include <string>
#include <cmath>
#include <deque>
#include <iomanip>
#include <fstream>

using namespace std;

//function
int main()
{
//Project number 2
//Declaration box
cout << "+----------------------------------------------------------------------------------------------------+" << endl;

cout << "* Application name: Project 2: Sequence of Things " << "*" << endl;
cout << "* Source files: JonesSequenceofThings.cpp " << "*" << endl;
cout << "* Author: Alex Jones " << "*" << endl;
cout << "* Description: Demonstration of 4 different User srand,setw, asterik diamonds, and squares completed " << "*" << endl;
cout << "+----------------------------------------------------------------------------------------------------+" << endl;
cout << "\n\n\n";

// This is task 1
//The objective is to display the expression Y = -3x + 2 from -5 to 5 in X and Y format with the output in a table

//This the header lines of code for X
string XXXheader = "X";
string XXXheader_ ="---------";

//These are the variables for the xvals and the yvals
int threeVal = 3, twoVal = -5;

//These are the header lines of code for Y
string YYheader = "Y";
string YYheader_ ="---------";

//These are the lines of code for the border using (---), (+), and setw
string taskheaderT = "+----------------------------------------------------------------------------------------------------+";
string taskheaderB = "+----------------------------------------------------------------------------------------------------+";
string taskOrd = "Task 1: Display Table for y = -3x + 2 from - 5 <= x <= 5 ";
cout << taskheaderT << endl << "|";
cout << setw(80) << taskOrd << setw(21) << "|" << endl << taskheaderB << endl;



//These lines of code are priting out the Y = -3 * x + 2 with -5 > 5 as the x in table form
//The equation is (-3*-5+2)

cout << setw(5) << XXXheader << setw(10) << YYheader << endl;
cout << setw(5) << XXXheader_ << setw(10) << YYheader_ << endl;
threeVal = (-3 * -5 + 2);
cout << setw(5) << twoVal << setw(10) << threeVal << endl;

threeVal = (-3 * -4 + 2);
cout << setw(5) << twoVal + 1 << setw(10) << threeVal << endl;

threeVal = (-3 * -3 + 2);
cout << setw(5) << twoVal + 2 << setw(10) << threeVal << endl;

threeVal = (-3 * -2 + 2);
cout << setw(5) << twoVal + 3 << setw(10) << threeVal << endl;

threeVal = (-3 * -1 + 2);
cout << setw(5) << twoVal + 4 << setw(10) << threeVal << endl;
cout << " \n\n";


//These are the lines of code showing the border for task 2 using cout function
cout << "+----------------------------------------------------------------------------------------------------+\n";

cout << "| Task 2: Display Table of the sqaure and cube for 1 -20 |\n";
cout << "+----------------------------------------------------------------------------------------------------+" << endl;
cout << "\n";

//These lines of code are using the constant int variable
//info will be dispayed in 3 rows & colums of 3

//These are the lines of code for the float varibles that go with the cube equation
float alpha = 1, bravo = 1;

//These are the string header and bottom lines of code along with code for border
string valHeader = "Value";
string valBottom_ = "------";

string sqHeader = "square";
string sqBottom_ = "------";

string cubeHeader = " cube";
string cubeBottom_ = "-------";

// defined variable intergers


cout << setw(4) << valHeader << setw(10) << sqHeader << setw(12) << cubeHeader << endl;
cout << setw(4) << valBottom_ << setw(10) << sqBottom_ << setw(12)<< cubeBottom_ << endl;

cout << setw(4) << alpha << setw(10) << pow(alpha, 2) << setw(12) << pow(alpha, 3) << "\n";
alpha++;
cout << setw(4) << alpha << setw(10) << pow(alpha, 2) << setw(12) << pow(alpha, 3) << "\n";
alpha++;
cout << setw(4) << alpha << setw(10) << pow(alpha, 2) << setw(12) << pow(alpha, 3) << "\n";
alpha++;
cout << setw(4) << alpha << setw(10) << pow(alpha, 2) << setw(12) << pow(alpha, 3) << "\n";
alpha++;
cout << setw(4) << alpha << setw(10) << pow(alpha, 2) << setw(12) << pow(alpha, 3) << "\n";
alpha++;
cout << setw(4) << alpha << setw(10) << pow(alpha, 2) << setw(12) << pow(alpha, 3) << "\n";
alpha++;
cout << setw(4) << alpha << setw(10) << pow(alpha, 2) << setw(12) << pow(alpha, 3) << "\n";
alpha++;
cout << setw(4) << alpha << setw(10) << pow(alpha, 2) << setw(12) << pow(alpha, 3) << "\n";
alpha++;
cout << setw(4) << alpha << setw(10) << pow(alpha, 2) << setw(12) << pow(alpha, 3) << "\n";
alpha++;
cout << setw(4) << alpha << setw(10) << pow(alpha, 2) << setw(12) << pow(alpha, 3) << "\n";
alpha++;
cout << setw(4) << alpha << setw(10) << pow(alpha, 2) << setw(12) << pow(alpha, 3) << "\n";
alpha++;
cout << setw(4) << alpha << setw(10) << pow(alpha, 2) << setw(12) << pow(alpha, 3) << "\n";
alpha++;
cout << setw(4) << alpha << setw(10) << pow(alpha, 2) << setw(12) << pow(alpha, 3) << "\n";
alpha++;
cout << setw(4) << alpha << setw(10) << pow(alpha, 2) << setw(12) << pow(alpha, 3) << "\n";
alpha++;
cout << setw(4) << alpha << setw(10) << pow(alpha, 2) << setw(12) << pow(alpha, 3) << "\n";
alpha++;
cout << setw(4) << alpha << setw(10) << pow(alpha, 2) << setw(12) << pow(alpha, 3) << "\n";
alpha++;
cout << setw(4) << alpha << setw(10) << pow(alpha, 2) << setw(12) << pow(alpha, 3) << "\n";
alpha++;
cout << setw(4) << alpha << setw(10) << pow(alpha, 2) << setw(12) << pow(alpha, 3) << "\n";
alpha++;
cout << setw(4) << alpha << setw(10) << pow(alpha, 2) << setw(12) << pow(alpha, 3) << "\n";
alpha++;
cout << setw(4) << alpha << setw(10) << pow(alpha, 2) << setw(12) << pow(alpha, 3) << "\n";
cout << "\n\n";
So I tried and it works can you guys tell me what you think of it? My instructor is what you call a difficult grader and he definitely is challenging. @Duthomhas what library is cstdio? and why are you not using cout?
@Duthomhas
Hard mode would be: "compute a table of stuff using a function, but without the assembly generated having any instructions that start with 'j'." :^)
@mollywhopping,
It's, err, rather long!

Are you SURE that your instructor said no loops, ... anywhere? I don't quite follow your logic for the "no loops, so I used setw".

I hate to tell you, but half your table is missing for the first task.
Last edited on
I'm positive he said no loops and where am I missing code? I triple checked it before I posted here and I just copied and ran the code and it works. I don't know how to make it shorter I am novice still figuring things out. I am going by the book in chapter order (Starting out with C++ from control structures through objects 9th edition). I would greatly appreciate any lessons.
Last edited on
where am I missing code?

You are apparently tasked with x between -5 and +5.
You are currently working it out for x between -5 and -1.



it works

It (might) compile and run. It's missing quite a few values (see above) and it is excessively long.



I don't know how to make it shorter

You should use loops to avoid massive repetition. For example you wrote exactly the same lines 20 times!
1
2
cout << setw(4) << alpha << setw(10) << pow(alpha, 2) << setw(12) << pow(alpha, 3) << "\n";
alpha++;

What would you do if you had to compute squares and cubes for x = 1 to 100, say? Would you really write those lines out 100 times?



with C++ from control structures

A loop is a fundamental control structure. In any programming language.




//These are the lines of code for the float varibles that go with the cube equation
float alpha = 1, bravo = 1;

You do have a funny book!



int threeVal = 3, twoVal = -5;

Hmmm!
Last edited on
You are doing too much work. Whenever you find yourself repeating something, write a function.

Also, please use code tags on the forum here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>

using namespace std;

const int width = 100;
void header(); void center( string ); void footer();
void table_1_header(); void table_1( int x );
void table_2_header(); void table_2( int x );


int main()
{
  header();
  center( "Application name: Project 2: Sequence of Things" );
  center( "Source files: JonesSequenceofThings.cpp"         );
  center( "Author: Alex Jones"                              );
  center( "Description: Math without loops"                 );
  footer();
  cout << "\n";

  header();
  center( "Task 1: Display Table for y = -3x + 2 from - 5 <= x <= 5 " ); 
  footer();
  table_1_header();
  table_1( -5 );
  table_1( -4 );
  table_1( -3 );
  table_1( -2 );
  table_1( -1 );
  table_1(  0 );
  table_1(  1 );
  table_1(  2 );
  table_1(  3 );
  table_1(  4 );
  table_1(  5 );
  cout << "\n\n";

  header();
  center( "Task 2: Display Table of the sqaure and cube for 1 - 20" );
  footer();
  table_2_header();
  table_2(  1 );
  table_2(  2 );
  table_2(  3 );
  table_2(  4 );
  table_2(  5 );
  table_2(  6 );
  table_2(  7 );
  table_2(  8 );
  table_2(  9 );
  table_2( 10 );
  table_2( 11 );
  table_2( 12 );
  table_2( 13 );
  table_2( 14 );
  table_2( 15 );
  table_2( 16 );
  table_2( 17 );
  table_2( 18 );
  table_2( 19 );
  table_2( 20 );

  cout << "\n\n";
}

// Centered-Box

void header() { cout << '+' << string( width - 2, '-' ) << "+\n"; }
void footer() { header(); cout << "\n"; }
void center( string s )
{
  int left = (width - 4 - s.size()) / 2 + s.size();
  int right = width - 2 - left;
  cout << "| " << setw(left) << s << setw(right) << "|" << "\n";
}

// Table 1: x, -3*x + 2

void table_1_header()
{
  cout <<
    "      X         Y    \n"
    "  --------- ---------\n";
}
void table_1( int x )
{
  int y = -3*x + 2;
  cout
    << setw(  7 ) << x
    << setw( 10 ) << y
    << "\n";
}

// Table 2: x, x**2, x**3

void table_2_header()
{
  cout <<
    "   value  squared  cubed \n"
    "  ------- ------- -------\n";
}
void table_2( int x )
{
  int squared = x*x;
  int cubed   = x*x*x;
  cout
    << setw(7) << x
    << setw(8) << squared
    << setw(8) << cubed
    << "\n";
}

See how all the work is reduced to just a couple of spots.

And... no loops. (Not even cheating ones like recursion.)
@Duthomhas So if voids aren't loops then are just repeated for a function? We just slightly covered loopa last night so it all still new to me. Thank you for the info @markanu but I tried compiling code from git and it would work even with the library and up to date studio's.
In C and C++ “void” is a magic word that means “no type”. Every function must have a return type. A void function returns nothing. Its return type is “no type”, or void.

That has nothing to do with loops. A loop is doing something repeatedly. When talking to programmers, that usually means using a while or for statement.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

void print_N_times( int N )
{
  for (int n = 0; n < N; n++)
    std::cout << (n + 1) << "\n";
}

int main()
{
  print_N_times( 10 );
}


But recursion is another kind of loop. It is just kind of hidden ;o)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

void print_N_times( int N )
{
  if (N > 0)
  {
    print_N_times( N - 1 );
    std::cout << n << "\n";
  }
}

int main()
{
  print_N_times( 10 );
}

You will mess with recursion later. For now, just know that it is a different kind of looping construct — and a great response when people say “no loops”.
3 lines.

1
2
3
4
5
6
7
int main()
{
//Task 1 : display a table for y = -3x + 2 from – 5 to 5.
int x = -5;
cout <<"X\t-5\t-4\t-3\t-2\t-1\t0\t1\t2\t3\t4\t5\t\nY";
cout <<"\t" << -3*(x++)+2<<"\t" << -3*(x++)+2<<"\t" << -3*(x++)+2<<"\t" << -3*(x++)+2<<"\t" << -3*(x++)+2<<"\t" << -3*(x++)+2<<"\t" << -3*(x++)+2<<"\t" << -3*(x++)+2<<"\t" << -3*(x++)+2<<"\t" << -3*(x++)+2<<"\t" << -3*(x++)+2;
}


its 'a' table format. /shrug. Bad question is bad.
thats the initial, brute force format, you can convolute it smaller.
Last edited on
jonnin pls
Just use (-5) (-4) ... (5) instead of using (x++) multiple times within the same sequence point.
(Undefined behavior pre-C++17, unspecified behavior post-C++17)
Last edited on
Ill need to review that; I thought stream operators were sequence points.
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

template<typename T> void write( T a ){ cout << a << '\t' << a * a << '\t' << a * a * a << '\n'; }
template<typename T, typename... Tail > void write( T a, Tail... b ){ write( a );   write( b... ); }

int main()
{
   cout << "x\tx^2\tx^3\n";
   write( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 );
}
Thank you all but as the responses progress the code becomes more and more advance as I haven't learned the materiel yet and it is a bit hard to decipher what everything means.
Do you have what you need, though?
Sometimes we play with a question a little, well beyond what is necessary... sorry if any of it was confusing.
Pages: 12