So basically i need to replace the two largest elements of an array, inputed by user. I don't understand why doesn't my code working.
#include <iostream>
using namespace std;
double Max (double a[], int n) {
double max1=0, max2=0;
for (int i=0; i<n; i++){
if(max1 < a[i]){
max2 = max1;
max1 =a[i];}
else
if(max2 < a[i]){
max2 = a[i];}
}
return max2,max1;
}
int main () {
setlocale(0,".1251");
double a[7];
cout<<"Input 7-digit array:\n";
for (int i=0; i<7; i++) cin >> a[i];
for (int i=0; i<7; i++) {
cout<<a[i]<<"\t";
}
system ("pause>>void");
return 0;
}
The function double Max (double a[], int n) is only defined, but you haven't called it. As it is right now, you are only outputting the same digits you inputted.
Also double Max (double a[], int n) can only return 1 double, so I'm assuming you were trying to return two variables return max2,max1; at once which isn't supported.
A fix to this could be by using a std::vector as data type and returning a 2 element array of max1 & max2 and then you would have to finish the code by actually calling the double Max (double a[], int n) function.
vector Maximums = Max(a[],7);
cout<< "\n max 1:\n" << Maximums[0] << "\n max 2:\n" << Maximums[1]
Related
I wrote the following code-
#include <iostream>
#include <tuple>
int sumarr(int64_t arr[], int length)
{
long long int sum= 0;
for(int i=0; i<length; i++)
{
sum += arr[i];
}
return sum;
}
void minimaxval(int64_t arr[], int length,int64_t *x,int64_t *y)
{
long long int c=0;
long long int d=10e9;
for(int j=0; j<5; j++)
{
if(arr[j] >= c)
{
c = arr[j];
}
if (arr[j] <= d)
{
d= arr[j];
}
}
*x=c;
*y=d;
}
int main()
{
int64_t arr[5];
int64_t p, q;
int64_t sum;
for(int k=0; k<5; k++){
std::cin>> arr[k];
if (arr[k]<1 || arr[k]>10e9){return 0;}
}
sum= sumarr(arr, 5);
minimaxval (arr, 5, &p, &q);
std::cout << sum-p << " " << (sum-q);
return 0;
}
This is my first question on stackoverflow. So I dont know a lot about format.
Input for the que. will be 5 space-separated integers.
Output should be the minimum sum and the maximum sum we can get by removing only one integer from the list.
It does not show any compiler issues.
It works fine for smaller integers but outputs a negative value for larger integers.
Where did I go wrong.
In 'summar' you have wrong return type. If you change it to int64_t everything will be fine.
P. S. And yes this is also my first answer)
I am getting an error in the word sum where it says average = sum / ARRAY_SIZE;. It is saying that the sum is undeclared. Even though I use sum previously in the getSum function, I only get the error here. I tried declaring it as a variable at the top but I am still getting that error. I do not know how else to declare it in a way that would stop this error.
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
// Function prototypes
int getHighest(int numbers[],int ARRAY_SIZE);
int getLowest(int numbers[],int ARRAY_SIZE);
int getSum(int numbers[],int ARRAY_SIZE);
int getAverage(int numbers[],int ARRAY_SIZE);
int main () {
// Variables
const int ARRAY_SIZE = 12; // Array size
int numbers [ARRAY_SIZE]; // Array with 12 integers
int count = 0; // loop counter variable
string filename;
// Open file
cout << "Enter input filename:";
cin >> filename;
ifstream inputFile(filename); // input file stream object
// Read numbers from file into array
while(count <ARRAY_SIZE && inputFile >> numbers[count])
count ++;
// Print results
cout<<ARRAY_SIZE<<" numbers read from input file."<<endl;
cout<<"The highest value is: "<<getHighest(numbers,ARRAY_SIZE)<<endl;
cout<<"The lowest value is: "<<getLowest(numbers,ARRAY_SIZE)<<endl;
cout<<"The sum of the numbers is: "<<getSum(numbers,ARRAY_SIZE)<<endl;
cout<<"The average of the numbers is: "<<getAverage(numbers,ARRAY_SIZE)<<endl;
}
int getHighest( const int numbers[], int ARRAY_SIZE)
{
int highest;
highest = numbers[0];
for(int count = 1 ; count < ARRAY_SIZE; count++)
{
if (numbers[count] > highest)
highest = numbers[count];
}
return highest;
}
int getLowest(const int numbers[], int ARRAY_SIZE)
{
int lowest;
lowest = numbers[0];
for (int count = 1; count < ARRAY_SIZE; count++)
{
if (numbers[count] < lowest)
lowest = numbers[count];
}
return lowest;
}
int getSum(const int numbers[], int ARRAY_SIZE)
{
int sum = 0;
for(int count = 0; count < ARRAY_SIZE; count++)
sum+= numbers[count];
return sum;
}
int getAverage(const int numbers[], int ARRAY_SIZE)
{
int average = 0;
for (int count = 0; count < ARRAY_SIZE; count++)
average = sum / ARRAY_SIZE;
return average;
}
It is saying that the sum is undeclared. Even though I use sum previously in the getSum function, I only get the error here.
sum is only a local variable in getSum so it cannot be accessed from getAverage, and in fact that variable does not exist anymore when you leave getSum
Note also the for in getAverage is useless because you do all the time average = sum / ARRAY_SIZE; whose value is always the same, so you want :
int getAverage(const int numbers[], int ARRAY_SIZE)
{
return getSum(numbers, ARRAY_SIZE) / ARRAY_SIZE;
}
Note your declarations and definitions do not correspond because in your definitions the array is const but not in your declarations. Put the array also const in the declarations :
int getHighest(const int numbers[],int ARRAY_SIZE);
int getLowest(const int numbers[],int ARRAYSIZE);
int getSum(const int numbers[],int SIZE);
int getAverage(const int numbers[],int SIZE);
may be also rename ARRAY_SIZE to size to not confuse with the global variable of the same name (even they receive its value), and SIZE to size because uppercase are generally not used to name a local variable/parameter.
Out of that, you are in C++, are you sure you want to use (C) arrays rather than for instance std::vector allowing to not give the size in argument to each functions and also to have iterators and predefined operations etc ?
Here is what I am trying to do, I want create a function that will compute and return (using by pass reference) the mean, maximum, and minimum. kinda new to c++ so I'd really appreciate the help.
Here exactly what I am trying to do:
Program describes what its suppose to do
Prompts the user to enter a number between 1 and 10, and then fills an array with positive floating point number
Outputs the contents of an array of floats, using a function
Use a function to compute the mean, max and min from an array of floats. The values are returned in pass by reference variables
Output the computed value
If at any point the user enters an invalid input, prompt them again
Terminates safely
Here is the code:
//include go here
#include <cstdio>
#include <iostream>
#include <cfloat>
using namespace std;
//Constants go here
const int MAX = 10;
const int MIN = 1
//outputs overview of program to user
void displayOverview();
//prompts user to enter a number between min and max and return it
//validated using a loop
int getIntInRange(int min, int max);
//prompts user to enter a floating point number that is > 0
//validated using a loop
float getPositiveFloat();
//prompts user for size of array (< size)
//fills nums with that many floating point values
int fillArray(float nums[], int size);
//outputs the array
void printArray (float arr[], int Size);
//Computes and returns the mean, maximum, and minimum
void computesValues(float arrr[], int size, float &mean, float &max, float &min);
int main(){
displayOverview();
float myArr[MAX];
int size = fillArray(myArr, MAX);
return 0;
}
//Prompt user to enter a number between Min and max
//If user entered a number within the range, is valid is true
//If user entered a number not within min and max, output sorry not in range
int getIntInRange(int min, int max){
int userInput = -1;
bool isValid = false;
while(!isValid){
printf("Please enter an integer between %d and %d\n", min, max);
scanf("%d", &userInput);
if(min <= userInput && userInput <= max){
isValid = true;
}else{
printf("Sorry, that is not in range\n Please try again\n");
}
}
return userInput;
}
//int numVals
int fillArray(float nums[], int size){
int numVals = getIntInRange(MIN, MAX);
for(int i=0; i< numVals&& i<size ; i++){
nums[i] = getPositiveFloat();
}
return numVals;
}
//Prompt user to enter a positive number
//if User enters a number that is not positive, output "Not a Positive"
float getPositiveFloat(){
float input;
do{
cout << "Please enter a positive number\n";
cin >> input;
if(!(input>0)){
cout << "Not a positive!\n";
}
}while(!(input>0));
return input;
}
//Introduction to the program
void displayOverview(){
cout << "Welcome to my program. You will see how magically I can compute things " <<
"from numbers!!" << endl;
}
//Print an array
void printArray(float arr[], int size){
for (int i = 0; i<size; i++){
cout << arr[i] << " ";
}
}
//Compute Min, max and mean.
void computesValues (float arr[], int size, float &mean, float &max, float &min){
}
void computesValues (float arr[], int size, float &mean, float &max, float &min)
{
float sum = 0.0;
for (int i = 0; i<size; i++){
sum = sum+ arr[i];
}
mean = sum/size;
max = arr[0];
for (int i = 1; i<size; i++){
if(arr[i] > max)
max = arr[i];
}
min = arr[0];
for (int i = 1; i<size; i++){
if(arr[i] < min)
min = arr[i];
}
printf("mean = %f max = %f min = %f\n", mean, max, min);
}
Mean is the average of the sum of the array elements: you have to add up the elements of the array and divide it by number of elements
To compute the maximum, set the first element as max, compare it with other elements and update it if any of the other elements exceeds it.
To compute the minimum, set the first element as min, compare it with other elements and update it if any of the other elements is less than it.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I wrote this code but not sure what to put in my main(), I tried my best to try and figure it out, if someone would please tell me what to do.
Here exactly what I am trying to do:
Program describes what its suppose to do
Prompts the user to enter a number between 1 and 10, and then fills an array with positive floating point number
Outputs the contents of an array of floats, using a function
Use a function to compute the mean, max and min from an array of floats. The values are returned in pass by reference variables
Output the computed value
If at any point the user enters an invalid input, prompt them again
Terminates safely
Here is the code:
//include go here
#include <cstdio>
#include <iostream>
#include <cfloat>
using namespace std;
//Constants go here
const int MAX = 10;
const int MIN = 1
//outputs overview of program to user
void displayOverview();
//prompts user to enter a number between min and max and return it
//validated using a loop
int getIntInRange(int min, int max);
//prompts user to enter a floating point number that is > 0
//validated using a loop
float getPositiveFloat();
//prompts user for size of array (< size)
//fills nums with that many floating point values
int fillArray(float nums[], int size);
//outputs the array
void printArray (float arr[], int Size);
//Computes and returns the mean, maximum, and minimum
void computesValues(float arrr[], int size, float &mean, float &max, float &min);
int main(){
displayOverview();
float myArr[MAX];
int size = fillArray(myArr, MAX);
return 0;
}
//Prompt user to enter a number between Min and max
//If user entered a number within the range, is valid is true
//If user entered a number not within min and max, output sorry not in range
int getIntInRange(int min, int max){
int userInput = -1;
bool isValid = false;
while(!isValid){
printf("Please enter an integer between %d and %d\n", min, max);
scanf("%d", &userInput);
if(min <= userInput && userInput <= max){
isValid = true;
}else{
printf("Sorry, that is not in range\n Please try again\n");
}
}
return userInput;
}
//int numVals
int fillArray(float nums[], int size){
int numVals = getIntInRange(MIN, MAX);
for(int i=0; i< numVals&& i<size ; i++){
nums[i] = getPositiveFloat();
}
return numVals;
}
//Prompt user to enter a positive number
//if User enters a number that is not positive, output "Not a Positive"
float getPositiveFloat(){
float input;
do{
cout << "Please enter a positive number\n";
cin >> input;
if(!(input>0)){
cout << "Not a positive!\n";
}
}while(!(input>0));
return input;
}
//Introduction to the program
void displayOverview(){
cout << "Welcome to my program. You will see how magically I can compute things " <<
"from numbers!!" << endl;
}
//Print an array
void printArray(float arr[], int size){
for (int i = 0; i<size; i++){
cout << arr[i] << " ";
}
}
//Compute Min, max and mean.
void computesValues (float arr[], int size, float &mean, float &max, float &min){
float sum = 0;
for (int i = 0; i<size; i++){
sum = sum + arr[i];
}
mean = sum/size;
max = arr[0];
for (int i = 1; i<size; i++){
if(arr[i] > max)
max = arr[i];
}
min = arr[0];
for (int i = 1; i<size; i++){
if(arr[i] < min)
min = arr[i];
}
printf("mean = %f max = %f min = %f\n", mean, max, min)
}
Main does not call to compute the values from your array.
void computesValues (float arr[], int size, float &mean, float &max, float &min)
Should make the last 3 Float variables local to the function and remove them from the prototype and declaration:
void void computesValues (float &arr[], int size){
float mean{}, max{}, min{};
You can call the printArray function should take const references of min, max, mean, array, and size variables.
void computesValues (float arr[], int size){
float min{}, max{}, mean{};
float sum = 0;
for (int i = 0; i<size; i++){
sum = sum + arr[i];
}
mean = sum/size;
max = arr[0];
for (int i = 1; i<size; i++){
if(arr[i] > max)
max = arr[i];
}
min = arr[0];
for (int i = 1; i<size; i++){
if(arr[i] < min)
min = arr[i];
}
printArray(arr, size, mean, max, min); // call from within your computesValues function
}
//Print an array
void printArray(const float arr[], const int size, const float mean, const float max, const float min){
for (int i = 0; i < size; i++){
cout << arr[i] << " ";
}
printf("mean = %f max = %f min = %f \n", mean, max, min);
}
The original errors with the code above:
printf("mean = %f max = %f min = %f\n", mean, max, min); //<- typo added semi-colon
const int MIN = 1; //<-typo added semi-colon
no declaration of variables min, max, mean in main.
const qualifier should be used for functions that do not modify the values
functions should only do one thing calc / print not both.
Demo
Question:
Write a function sumArray() that has as parameters two arrays A and B and calculates and stores the sum of corresponding elements of the arrays. Include any additional parameter(s) you consider necessary.
Write a main() function that uses the function inputArray() in the previous slide to perform the input of two integer arrays X1 and X2 and uses sumArray() to calculate the sum of corresponding values of X1 and X2. It then displays the calculated values.
I was searching a way to return multiple values from a function but still it is not working even after trying some examples from SO. can anyone help me with it i just start learning it.
Problem: i can't figure out how to get the values of sumA and sumB in main().
Here are my codes:
#include <iostream>
using namespace std;
int sumArray(int a[], int b[]){
int sumA=0, sumB=0;
for(int i=0; i<4; i++){
sumA += a[i];
sumB += b[i];
}
return sumA,sumB;
}
void inputArray(int arg[], int n){
for(int i=0; i<n; i++){
cin>>arg[i];
}
}
int main(){
int firstarray[4];
int secondarray[4];
int l=4;
cout<<"Input 4 values for the array 1: ";
inputArray(firstarray,l);
cout<<"Input 4 values for the array 2: ";
inputArray(secondarray,l);
sumArray(firstarray,secondarray);
cout<<"sum of array 1 is: "<<firstarray<<endl;
cout<<"sum of array 2 is: "<<secondarray<<endl;
return 0;
}
This is the only way i have learned it.
You could accept references as 2 additional arguments. You can then set sumA and sumB to the correct values.
void sumArray (int a[], int b[], int & sumA, int & sumB)
{
sumA=0;
sumB=0;
for(int i=0; i<4; i++){
sumA += a[i];
sumB += b[i];
}
}
Then you can use it like
int firstarray[4];
int secondarray[4];
// do your stuff here
int sumA, sumB;
sumArray(firstarray,secondarray, sumA, sumB);
cout<<"sum of array 1 is: "<<sumA<<endl;
cout<<"sum of array 2 is: "<<sumB<<endl;
std::tuple<int,std::string> return_two()
{
return std::make_tuple(42, "don't panic");
}
auto sval = std::string{};
auto ival = 0;
std::tie(ival,sval) = return_two();
Why not just call that function twice?
Change your sumArray function to:
int sumArray(int array[]){
int sum=0;
for(int i=0; i<4; i++){
sum += array[i];
}
return sum;
}
Then for the rest of your code:
void inputArray(int arg[], int n){
for(int i=0; i<n; i++){
cin>>arg[i];
}
}
int main(){
int firstarray[4];
int secondarray[4];
int l=4;
cout<<"Input 4 values for the array 1: ";
inputArray(firstarray,l);
cout<<"Input 4 values for the array 2: ";
inputArray(secondarray,l);
cout<<"sum of array 1 is: "<<sumArray(firstArray)<<endl;
cout<<"sum of array 2 is: "<<sumArray(secondArray)<<endl;
return 0;
}
If you can't do it that way, then you can return a pointer to the first part of an array.
As seen here: Return array in a function