Avatar of edelossantos
edelossantos
 asked on

lab4.cpp Code Syntax and Algorithm Check

Is this correct?  Please advise.  Del
*******************************Actual Code*********************************
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cmath>

int biggest(const int, const int, const int);
int gcd(const int, const int);
void addBlanks(){};
double round(const double);


int main() {

  rand();
  srand(time(0));
  time(0);

  int x, y, z, randOut, srandOut;
  cout << "Enter three integers. " << endl;
  cin >> x >> y >> z;

  cout << "The biggest of " << x << "," << y << ", and " << z << "is " << biggest(x,y,z) << endl;
  cout << "The greatest comon divisor of " << x << " and " << y << " is " << gcd(x,y) << endl;
  cout << randOut << " is a random number. " << endl;
  cout << srandOut << " is a seeded random number. " << endl;
  cout << "The rounded number is " << round(floatNum) << endl;

  return 0;

}

int biggest(const int x, const int y, const int z) {
 
  int size;
  if((x < size) && (y < size) && (z < size))
    return size;

}

int gcd(const int x, const int y) {
   
   int remainder = x % y;
     if(remainder != 0)
     return gcd(remainder, x);
    return x;

}

void addBlanks() {

  //generate random #, print digits seperated by 5 spaces
 
  double firstDigit = digit;
  for(int digit = 4; digit >= 0; digit--)
    cout << firstDigit << " ";

  return;

}

double round(const double floatNum) {

  //round parameter to the nearest int
  floor(floatNum);
  pow(floatNum,);

}

**************************************************************************************
 
output:

lab4.cpp: In function `double round(double)':
lab4.cpp:67: parse error before `)'
 
I am not sure on addBlanks() and double round()...please advise.

Description of the problem below:
*************************************************************************************
PART I.
Write function definitions for the following prototypes:

int biggest(const int, const int, const int );
// Returns the largest of three integers.

int GCD( const int, const int );
// returns the greatest common divisor (GCD) of two integers, where GCD is the largest integer that divides both of them evenly.

void addBlanks ( );
/* produce a random integer between 1 and 32767 and print it as a series of digits, where each digit is separated by a space; e.g. 4562 should be printed as: 4 5 6 2 (text 3.25(c) pg 244)*/

PART II.
Write the prototype, definition and call for a function that will take a local double from main and round it to its closest integer value; e.g. 3.45 will become 3.00, and any number >= 3.5 will become 4.00. See pg. 242 Exercise 3.13 for help. The function should modify the value of the double in main.



 


C++

Avatar of undefined
Last Comment
jkr

8/22/2022 - Mon
SOLUTION
Axter

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Axter

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Sys_Prog

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Axter

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Axter

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Sys_Prog

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Sys_Prog

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
edelossantos

ASKER
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cmath>

int biggest(const int, const int, const int);
int gcd(const int, const int);
void addBlanks();
double round(const double);


int main() {

  rand();
  srand(time(0));
  time(0);

  int x, y, z, randOut, srandOut, floatNum;
  cout << "Enter three integers. " << endl;
  cin >> x >> y >> z;

  cout << "The biggest of " << x << "," << y << ", and " << z << "is " << biggest(x,y,z) << endl;
  cout << "The greatest comon divisor of " << x << " and " << y << " is " << gcd(x,y) << endl;
  cout << randOut << " is a random number. " << endl;
  cout << srandOut << " is a seeded random number. " << endl;
  cout << "The rounded number is " << round(floatNum) << endl;

  return 0;

}

int biggest(const int &x, const int &y, const int &z){
      int max = x ;
      if ( y > x ) {
          max = y ;
      }
      if ( z > max ) {
             max = z ;
       }  
     return max ;
}

int gcd(const int x, const int y) {
   
   int remainder = x % y;
   if(x > y) {
         remainder = y;
   }
     if(remainder != 0)
     return gcd(remainder, x);
    return x;

}

void addBlanks() {

  //generate random #, print digits seperated by 5 spaces
  int digit;
  double firstDigit = digit;
  for(int digit = 4; digit >= 0; digit--)
    cout << firstDigit << " ";

  return;

}

double round(const double floatNum) {

  //round parameter to the nearest int
  floor(floatNum);
  cout << pow(double floatNum);

}

g++

lab4.cpp: In function `double round(double)':
lab4.cpp:72: parse error before `)'




 


 


edelossantos

ASKER
correction>>

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cmath>

using namespace std;

int biggest(const int, const int, const int);
int gcd(const int, const int);
void addBlanks();
double round(const double);


int main() {

  rand();
  srand(time(0));
  time(0);

  int x, y, z, randOut, srandOut, floatNum;
  cout << "Enter three integers. " << endl;
  cin >> x >> y >> z;

  cout << "The biggest of " << x << "," << y << ", and " << z << "is " << biggest(x,y,z) << endl;
  cout << "The greatest comon divisor of " << x << " and " << y << " is " << gcd(x,y) << endl;
  cout << randOut << " is a random number. " << endl;
  cout << srandOut << " is a seeded random number. " << endl;
  cout << "The rounded number is " << round(floatNum) << endl;

  return 0;

}

int biggest(const int &x, const int &y, const int &z){
      int max = x ;
      if ( y > x ) {
          max = y ;
      }
      if ( z > max ) {
             max = z ;
       }  
     return max ;
}

int gcd(const int x, const int y) {
   
   int remainder = x % y;
   if(x > y) {
         remainder = y;
   }
     if(remainder != 0)
     return gcd(remainder, x);
    return x;

}

void addBlanks() {

  //generate random #, print digits seperated by 5 spaces
  int digit;
  double firstDigit = digit;
  for(int digit = 4; digit >= 0; digit--)
    cout << firstDigit << " ";

  return;

}

double round(const double floatNum) {

  //round parameter to the nearest int
  floor(floatNum);
  cout << pow(double floatNum);

}

 


 


SOLUTION
jkr

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Axter

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
jkr

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Axter

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
jkr

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Axter

>>you haven't assigned any values to the variables you're outputting. And 'biggest()' should be

That's what I first thought too, but he has the following line before calling biggest()
cin >> x >> y >> z;
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
jkr

>>That's what I first thought too, but he has the following line before calling biggest()

Err, that's not what I meant - it is declared to take ints by value, and the implementation uses references. That I tried to point out.
edelossantos

ASKER
lab4.cpp: In function `int main()':
lab4.cpp:38: parse error before `double'

gcd isn't behaving correctly, I don't think my seed is correct.

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cmath>

using namespace std;

int biggest(const int, const int, const int);
int gcd(const int, const int);
void addBlanks();
double round(const double);


int main() {

  rand();
  srand(time(0));
  time(0);

  int x, y, z, randOut, srandOut, floatNum;
  unsigned seed;
  srand(seed);

  for(int counter = 1; counter <= 10; counter++) {
    cout << setw(10) << (1 + rand() % 6);
    if(counter % 5 == 0)
      cout << endl;

  }
  cout << "Enter three integers. " << endl;
  cin >> x >> y >> z;

  cout << "The biggest of " << x << "," << y << ", and " << z << "is " << biggest(x,y,z) << endl;
  cout << "The greatest comon divisor of " << x << " and " << y << " is " << gcd(x,y) << endl;
  cout << randOut << " is a random number. " << endl;
  cout << srandOut << " is a seeded random number. " << endl;
  cout << "The rounded number is " << round(const double floatNum) << endl;

  return 0;

}

int biggest(const int x, const int y, const int z){
     int max = x ;
     if ( y > x ) {
         max = y ;
     }
     if ( z > max ) {
            max = z ;
      }  
     return max ;
}

int gcd(const int x, const int y) {
   
   int remainder = x % y;
   if(x > y) {
         remainder = y;
   }
     if(remainder != 0)
     return gcd(remainder, x);
    return x;

}

void addBlanks() {

 //generate random #, print digits seperated by 5 spaces
 int digit;
 double firstDigit = digit;
 for( digit = 4; digit >= 0; digit--)
   cout << firstDigit << " ";

 return;

}

double round(const double floatNum) {

double tmp = floatNum+ 0.5;

const double n = floatNum;

 return (double) n;

}




 


 


ASKER CERTIFIED SOLUTION
jkr

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.