Link to home
Create AccountLog in
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.



 


SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of edelossantos
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 `)'




 


 


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
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
>>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;
>>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.
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
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account