Arrays Assignment Help

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).
I don't know how to put the code in

You press keys on the keyboard?


You do have to show some effort. Try to do one step at a time.
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
constexpr int 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); 
}

Last edited on
This is my code so far, but I need help on printing all of the 10 numbers that the user enters.

#include <iostream> // for input and ouput (cin / cout)

using namespace std;

// constants
const int ARRAY_SIZE = 10;

void getData();

void printData();

int main() {
cout << "Enter 10 integers (separated by a space): ";

getData();
printData();

return 0;
}

void getData() {
int z, i;
int temp = 0;
int myarray[10] = { 1,2,3,4,5,6,7,8,9,10 };

for (z = 0; z < 10; z++) {
cin >> myarray[z];
}
}

void printData() {


}
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.

Here is my modified code. I just don't understand why it is not printing out all 10 integers.

constexpr int sizeArray = 10; //global constant

void getData(int* ip);

void printData(int* ip);

int main() {
int array[sizeArray]; //sizeArray can't be a variable, it must be a constant in standard c++
getData(array);
}

void getData(int* ip) {
cout << "Enter " << sizeArray << " integers (separated by a space): ";
for (int x = 0; x < sizeArray; x++)
cin >> ip[x];
}

void printData(int* ip) {
for (int x = 0; x < sizeArray; x++)
cout << ip[x];
}
You have created a function printData() to print an array ... but you don't actually use it! You need to call it from main().

PLEASE USE code TAGS.
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

You entered: 5 0 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460

Lowest element : -858993460
Highest element : 5
Odd counts : 1
Sum of array : 1717986917
Duplicate elements : -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460
Enter 10 integers (separated by a space): 5, 10, 15, 20, 25, 30, 35, 40, 45, 50

Do you call , "a space"?
Yep, you are right. That is a mistake. But what is affecting my code to print out random numbers?
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.

If you do notice that a stream is in failed state, then you can call clear() on it.
http://www.cplusplus.com/reference/ios/ios/clear/

You would obviously have to get the comma out of the stream before you can read an another integer.
It works! I just had to remove the commas, like you said. Small things can just ruin your program. Thank you for the help and advice!
Topic archived. No new replies allowed.