I need help with this arrays assignment. I understand how arrays work, but I don't know how to put the code in. Help will be much appreciated for me to understand how to do it. Thank you!
Write a program (named arrayfun) that gets 10 integers from the user, stores them in an array, and then prints out:
the entire array
the lowest value in the array
the highest value in the array
the number of odd numbers in the array
the sum of all the values in the array.
all the values that appear in the array more than once
Details
You must write/use the following functions (you’ll need to determine the parameter list and return value for each):
getData() - fills in given array with data entered by the user
printData() - prints given array values to the screen (space delimited)
lowestValue() - returns the lowest value in the given array
highestValue() - returns the highest value in the given array
countOdds() - returns the number of odd numbers in the given array
sum() - returns the sum of all the values in the given array
getDuplicateValues ()
This will accept two arrays (one input and one output) and a size (of the arrays).
All the values that appear multiple times in the input array will be copied to the output array.
It will also need to return how many elements were placed in the output array
any other functions you feel are useful/necessary.
Notes: Output to the screen should only be done in main() and printData() (and the prompt in getData).
if you know how they work, you know how to add them to the code. Not being a jerk here, just trying to open your mind to the underlying problem.
Ill help a tiny bit to get you started, then you need to show code and ask a question that will let YOU do the work.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
constexprint size = 10; //global constant
void getdata(int* ip)
{
cout << "input "<<size << " ints" << endl;
for(int x = 0; x < size; x++)
cin >> ip[x];
}
…
main
{
int array[size]; //size can't be a variable, it must be a constant in standard c++
getdata(array);
}
its virtually identical to the getdata I gave, except cin and cout change, and the words around it if any change.
I gave it to you and you went off on your own, and its wrong. I applaud you trying your own version, but fix get-data first.
- get data has no parameters, so everything it does is thrown away. myarray is destroyed when get-data completes; it is a 'local' variable that is created and destroyed inside that function.
- the array should be created in main, and passed to the functions, as shown in my first post.
So, I filled out the entire program out, but have a problem where it messes with the numbers, for some reason. I don't know how it is affecting it. I will show you the problem, but here is my code.
Code:
#include <iostream>
using namespace std;
void getData(int array[], int size);
void printData(int array[], int size);
int lowestValue(int array[], int size);
int highestValue(int array[], int size);
int countOdds(int array[], int size);
int sum(int array[], int size);
int* getDuplicateValues(int array[], int out[], int size);
int main() {
int array[10], out[10];
getData(array, 10);
printData(array, 10);
int low = lowestValue(array, 10);
int high = highestValue(array, 10);
int odds = countOdds(array, 10);
int sum1 = sum(array, 10);
int *out1 = getDuplicateValues(array, out, 10);
cout << "Lowest element : " << low << endl;
cout << "Highest element : " << high << endl;
cout << "Odd counts : " << odds << endl;
cout << "Sum of array : " << sum1 << endl;
cout << "Duplicate elements : ";
for (int i = 0; i < 10; i++)
if (*(out + i) != 1)
cout << *(out + i) << " ";
cout << endl;
return 0;
}
void getData(int array[], int size) {
cout << "Enter 10 integers (separated by a space): : " << endl;
for (int i = 0; i < size; i++)
cin >> array[i];
}
void printData(int array[], int size) {
for (int i = 0; i < size; i++)
cout << array[i] << " ";
cout << endl;
}
int lowestValue(int array[], int size) {
int min = array[0]; //set first element as min
for (int i = 1; i < size; i++) //iterates throw all the elements in array
if (array[i] < min) //if any value is found less than min
min = array[i]; //update min
return min; //return min to called function
}
int highestValue(int array[], int size) {
int max = array[0]; //set first element as max
for (int i = 1; i < size; i++) //iterates throw all the elements in array
if (array[i] > max) //if any value is found greater than max
max = array[i]; //update max
return max; //return max to called function
}
int countOdds(int array[], int size) {
int count = 0;
for (int i = 0; i < size; i++) //iterates throw all the elements in array
if (array[i] % 2 != 0) //counts odd numbers
count++;
return count; //return count
}
int sum(int array[], int size) {
int sum = 0;
for (int i = 0; i < size; i++) //iterates throw all the elements in array
sum += array[i]; //summation of all elements
return sum; //return sum
}
int* getDuplicateValues(int array[], int out[], int size) {
for (int i = 0; i < size; i++)
out[i] = 1;
for (int i = 0; i < size; i++)
{
int count = 1;
for (int j = i + 1; j < size; j++)
{
if (array[i] == array[j])
count++;
}
if (count > 1)
out[i] = array[i];
}
return out;
}
In console:
Enter 10 integers (separated by a space): 5, 10, 15, 20, 25, 30, 35, 40, 45, 50
The first cin >> starts reading the stream. It wants an integer.
First character is '5' and cin goes: "Great! Lets do this!"
The next character is the comma. Cin says: "Oh, we are done here." and stores number 5 into variable. The comma stays in the stream.
The second cin >> starts reading the stream. It wants an integer.
First character is ',' and cin goes: "That is no integer. Frak, I'm going home."
IIRC, the new C++ puts 0 into the variable.
The third (subsequent) cin >> goes "lalalalalala I ain't talking to you". It has gone home. Upset. In failed state. Nothing touches the variable.
Nothing touches the variable. The variable keeps its previous state. What was it? int array[10]; // uninitialized
Uninitialized, undefined, unknown, whatever.