Help adding numbers to an array and printing them?
-
For this program I want to: 1) Create an array dynamically with the requested size. 2) Every time I read in a new number, check first to see if it is already in the array. If so, discard the number and ask for the next number. If not in the array, place the number in the array. 3)Use a counter to count how many numbers you have added to the array. Use this counter to compute the array index of the next number to add to the array. 4) Print the numbers in the array This is the code I have so far, any ideas on where I am going wrong? #include <iostream> using namespace std; bool compare(int arrayNum[],int arraySize,int element) { for (int i = 0; i < arraySize; i++) { if(arrayNum[i]==element) return false; } return true; } int main() { cout << "How many numbers?" << endl; int n; cin >> n; int *num = new int [ n ]; int arrayElement; int count = 0; int i = 1; cout << "Please input " << n << " numbers: " << endl; cin >> arrayElement; //while ( i <= n) //int list = 0; if ( i == 1) num[0] = arrayElement; else if (compare(num, n ,arrayElement)) { count++; num[count]=arrayElement; } i++; //displays array elements for (int j = 0; j <= count ;j++) cout<<num[j]<<endl; return 0;
-
Answer:
A few things: . you don't need a special case for the first number added to the array . your display loop goes too far, it should be while j < count, not <= . I'd like to see some user input validation . the function 'compare' should have a better name . I don't like multiple returns in a function In real life, an STL set<int> can do a lot of this work for you, but I'm guessing this is an exercise where you need to code the logic yourself. So, here are some improvements I'd recommend: #include <iostream> #include <string> #include <sstream> using namespace std; int getInt(const string &prompt); bool isInArray(int arrayNum[],int arraySize, int element) { bool found = false; for (int i = 0; i < arraySize; i++) { if ((found = (arrayNum[i] == element)) == true) { break; } } return found; } int main(int argc, char *argv[]) { int n, *num, arrayElement, count = 0; do { n = getInt("How many numbers? : "); } while (n < 1); num = new int[n]; cout << "Please input " << n << " numbers: " << endl; for (int i = 0; i < n; i++) { if (isInArray(num, count, arrayElement = getInt("> ")) == false) { num[count++] = arrayElement; } } cout << "Unique numbers entered: " << endl; for (int j = 0; j < count; j++) { cout << num[j] << endl; } return 0; } int getInt(const string &prompt) { stringstream ss; string line; bool inputOk = false; int n; do { cout << prompt; getline(cin,line); ss.clear(); ss.str(line); if ((!(ss >> n)) || (ss.good())) { cout << "invalid input, try again" << endl; } else { inputOk = true; } } while (inputOk == false); return n; }
Sarah S at Yahoo! Answers Visit the source
Other answers
Where is your loop? Post the code again with your while loop uncommented, and curly braces around the body of the loop. It looks pretty much correct (if there is an error it isn't glaringly obvious) if you just had the loop working.
John H
Related Q & A:
- Is there a recommended and consistent way of adding social buttons?Best solution by User Experience
- How to post an array of complex objects (that has an array of complex objects) with jQuery?Best solution by Stack Overflow
- how can i remove an array from an array?Best solution by Stack Overflow
- The printer was not printing in black ink! Help?Best solution by Yahoo! Answers
- Is there a way where u can send someone an email using hotmail and adding a small picture in the?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.