I have to find the elements in a given array, and I found a program in other site, but when I try to interpret the code in my way, I have error.
That is from the other site:
#include <iostream>
using namespace std;
// function to return sum of elements
// in an array of size n
int sum(int arr[], int n)
{
int sum = 0; // initialize sum
// Iterate through all elements
// and add them to sum
for (int i = 0; i < n; i )
sum = arr[i];
return sum;
}
int main()
{
int arr[] = { 12, 3, 4, 15 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Sum of given array is " << sum(arr, n);
return 0;
}
That's mine:
#include <iostream>
using namespace std;
int Function(int arr[], int Broi)
{
int suma = 0;
for (int i = 0; i < Broi; i ) {
cin >> arr[Broi];
suma = arr[i];
}
return suma;
}
int main()
{
int arr;
int Broi = sizeof(arr) / sizeof(arr[0]);
cout << "Srednoto Aritmetichno e: " << Function(arr[], broi);
return 0;
}
Also the first programe gives the numbers of the array, but I want the user to cin>> them when u write the length of the arr.
CodePudding user response:
Your code is not declaring an array at all. It is declaring a single int
.
You said in comments that:
The task is to read 10 numbers, and to put them in array
So, you should declare a 10-int
array in main()
and fill it with user values before passing it to Function()
, similar to how the original code was doing. The user input doesn't really belong in Function()
to begin with.
Try something more like this instead:
#include <iostream>
using namespace std;
int Function(int arr[], int Broi)
{
int suma = 0;
for (int i = 0; i < Broi; i ) {
suma = arr[i];
}
return suma;
}
int main()
{
int arr[10];
cout << "Enter 10 numbers: ";
for(int i = 0; i < 10; i){
cin >> arr[i];
}
cout << "The sum is: " << Function(arr, 10);
return 0;
}
CodePudding user response:
The line arr[Broi]
is accessing the array out of bounds, causing undefined behavor, because the array consists only of a single element. Even if it consisted of Broi
elements, it would be accessing the array out of bounds, because valid indexes would be 0
to Broi - 1
.
In the comments section (but not in the question), you stated that you are supposed to read 10
numbers from the user. If arr
were pointing to an array of 10
elements instead of 1
element, then it would make sense to write arr[i]
instead of arr[Broi]
.
The simplest solution to the problem would be to not use arrays at all:
#include <iostream>
constexpr int NUM_INPUTS = 10;
int main()
{
int input;
int sum = 0;
for ( int i = 0; i < NUM_INPUTS; i )
{
//prompt user for input
std::cout << "Please enter integer #" << i 1 << ": ";
//attempt to read integer from user
if ( ! ( std::cin >> input ) )
{
std::cout << "Input failure!\n";
return 1;
}
//add user input to sum
sum = input;
}
//print sum
std::cout << "\nThe sum of all numbers is: " << sum << ".\n";
return 0;
}
However, since you stated in the comments section that you are supposed to use arrays, then you are probably supposed to first read all 10
numbers from std::cin
into an array of 10
elements and then calculate the sum afterwards:
#include <iostream>
#include <cstdlib>
constexpr int NUM_INPUTS = 10;
void input_array( int arr[] )
{
for ( int i = 0; i < NUM_INPUTS; i )
{
//prompt user for input
std::cout << "Please enter integer #" << i 1 << ": ";
//attempt to read integer from user
if ( ! ( std::cin >> arr[i] ) )
{
std::cout << "Input failure!\n";
std::exit( EXIT_FAILURE );
}
}
}
int calculate_sum( int arr[] )
{
int sum = 0;
for ( int i = 0; i < NUM_INPUTS; i )
{
sum = arr[i];
}
return sum;
}
int main()
{
int arr[NUM_INPUTS];
//fill array with user input
input_array( arr );
//print sum
std::cout << "\nThe sum of all numbers is: " << calculate_sum( arr ) << ".\n";
return 0;
}
Both programs have the following output:
Please enter integer #1: 20
Please enter integer #2: 30
Please enter integer #3: 10
Please enter integer #4: 5
Please enter integer #5: 31
Please enter integer #6: 17
Please enter integer #7: 6
Please enter integer #8: 14
Please enter integer #9: 18
Please enter integer #10: 50
The sum of all numbers is: 201.
CodePudding user response:
The simple fix without too many complicated features would be like below:
#include <iostream>
int Function( int* const array, const std::size_t elementCount)
{
int suma { };
for ( std::size_t idx = 0; idx < elementCount; idx )
{
std::cin >> array[ idx ];
suma = array[ idx ];
}
return suma;
}
int main( )
{
int myArray[ 4 ] { };
int suma = Function( myArray, sizeof( myArray ) / sizeof( *myArray ) );
std::cout << "Srednoto Aritmetichno e: " << suma << '\n';
return 0;
}
That's it. You can change the size of myArray
to anything that fits onto stack memory.