/* dolphins
* Write a program that takes one and only one comand line argument: the number of dolphins to be trained (>0).
* Allow the trainer to enter an age for each dolphin (>0). To get an age call a function with
* the following prototype:
* int* getAge(void)
* All ages should be stored in array in main.
* Finally, print out the age of the oldest dolphin.
* pseudocode
* Use int main(int argc, string argv[])
* command-line argument will be the number of dolphins
* test is number > 0
* if <= 0, exit
* if > 0, prompt traner to enter age of each dolphin
* use the function int* getAge(void)
* store the ages in an array in main: use int age[10];
* Find the oldest dolphin and print the result
*/
#include <cstdio>
#include <string>
#include <cstdlib>
usingnamespace std;
// function prototype
int* getAge(void);
int main(int argc, string argv[])
{
// array to store ages
int age[50];
// change agrv[1] to a number using atoi
int n_Dolphins = atoi(agrv[1]);
// check if number > 0
if (n_Dolphins > 0)
{
for (int i = 0; i < n_Dolphins; i++)
{
age[i] = *getAge();
}
}
else
{
exit(EXIT_FAILURE);
}
// find the oldest
int oldest = 0;
for (int i = 0; i < n_Dolphins; i++)
{
if (age[i] > oldest)
{
oldest = age[i];
}
}
printf("The oldest dolphin's age is: %i\n", oldest);
}
// dolphins
/* Write a program that takes one and only one comand line argument: the number of dolphins to be trained (>0).
* Allow the trainer to enter an age for each dolphin (>0). To get an age call a function with
* the following prototype:
* int* getAge(void)
* All ages should be stored in array in main.
* Finally, print out the age of the oldest dolphin.
* pseudocode
* Use int main(int argc, string argv[])
* command-line argument will be the number of dolphins
* test is number > 0
* if <= 0, exit
* if > 0, prompt traner to enter age of each dolphin
* use the function int* getAge(void)
* store the ages in an array in main: use int age[10];
* Find the oldest dolphin and print the result
*/
#include <cstdio>
#include <string>
#include <cstdlib>
#include <iostream>
usingnamespace std;
// function prototype
int* getAge(void);
// Gobal variable
int xx = 1;
int main(int argc, char* argv[])
{
// array to store ages
int age[50];
// change agrv[1] to a number using atoi
int n_Dolphins = atoi(argv[1]);
// check if number > 0
if (n_Dolphins > 0)
{
for (int i = 0; i < n_Dolphins; i++)
{
age[i] = *getAge();
}
}
else
{
exit(EXIT_FAILURE);
}
// find the oldest
int oldest = 0;
for (int i = 0; i < n_Dolphins; i++)
{
if (age[i] > oldest)
{
oldest = age[i];
}
}
printf("The oldest dolphin's age is: %i\n", oldest);
}
int* getAge(void)
{
int x = 0;
int* dolage = &x;
printf_s("Please enter age of dolphin number %i: ", xx);
cin >> x;
xx++;
return dolage;
}