Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,430 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,278 people online right now. Registration is fast and FREE... Join Now!




Array Problem

 
Reply to this topicStart new topic

Array Problem

captainhampton
22 Jan, 2008 - 07:29 PM
Post #1

Jawsome++;
Group Icon

Joined: 17 Oct, 2007
Posts: 518



Thanked: 2 times
Dream Kudos: 825
My Contributions
Create a 10 number array and initialize each element of the array to 0. Read in 10 numbers, each of which is between 10 and 100, inclusive. As each number is read, validate it and store it in the array only if it is not a duplicate of a number already read. After reading all the values, display only the unique values that the user entered.

Got a start, no idea how to see if it is a duplicate or not, any help would be appreciated:

CODE

#include <cstdlib>
#include <iostream>

using namespace std;

//Lab 3 -- Problem # 2

int main(int argc, char *argv[])
{
    int array[10];
    int counter = 0;
    
    for ( int i = 0; i < 10; i++ ){
        array[i] = 0;
        }//end for
        
    cout << "Enter numbers between 10 and 100"<<endl;
    for ( int i = 0; i < 10; i++ ){
           cin >> array[i];
        }//end for  
        
    
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Array Problem
22 Jan, 2008 - 07:45 PM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Well loop through the array and check if the number is equal with the elements of the array, if yes, the do not store it if not then store it.
I can see from your code you are assigning the values directly to the array. I suggest you use some temporary variable, and validate it first, then store its value.
User is offlineProfile CardPM
+Quote Post

#include<wmx010>
RE: Array Problem
22 Jan, 2008 - 08:21 PM
Post #3

D.I.C Head
**

Joined: 19 Jan, 2008
Posts: 75



Thanked: 1 times
My Contributions
Hope this helps:

CODE

#include <iostream>
#include <cstdlib>
using namespace std;

int main(int argc, char *argv[])
{
    const int ARRAY_SIZE = 10;
    int a[ARRAY_SIZE];

    int counter = 0;
    
    for ( int i = 0; i < ARRAY_SIZE; i++ )
    {
        a[i] = 0;
    }
        
    cout << "Enter numbers between 10 and 100"<<endl;
    for ( int i = 0; i < ARRAY_SIZE; i++ )
    {
        cin >> a[i];
    }

    // Display elements in array
    for (int i = 0; i < ARRAY_SIZE; i++)
    {
        if (a[i] == a[i+1])
            continue; // continue may only be used inside a loop, such as a for statement.
                      // ..It terminates the current iteration of the loop and proceeds directly to the next.

        cout << " " << a[i];
    }

    cout << "\n\n";

    system("PAUSE");
    return EXIT_SUCCESS;
}

User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Array Problem
22 Jan, 2008 - 08:44 PM
Post #4

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
#include<wmx010> I don't think your solution meets the assignment requirements.
How about if the user enter two same numbers but they are not adjacent,
e.g. user enters:
1 2 3 4 1?

here is my suggestion in code:
*note I haven't compile it
CODE

#include <iostream>
#include <cstdlib>
using namespace std;

bool isNotInTheArray(int temp, int a[])
{
     for ( int i = 0; i < 10; i++ )
    {
        if(a[i]==temp) return false;
    }
    return true;
}

int main(int argc, char *argv[])
{
    const int ARRAY_SIZE = 10;
    int a[ARRAY_SIZE];

    int counter = 0;
    int temp;
    
    for ( int i = 0; i < ARRAY_SIZE; i++ )
    {
        a[i] = 0;
    }
        
    cout << "Enter numbers between 10 and 100"<<endl;
    for ( int i = 0; i < ARRAY_SIZE; i++ )
    {
        cin >> temp;
        
        if(temp>=10 && temp<=100)
              if(isNotInTheArray(temp, a))    a[i]=temp;
    }

    // Display elements in array
    for (int i = 0; i < ARRAY_SIZE; i++)
    {
        cout << " " << a[i];
    }

    cout << "\n\n";

    system("PAUSE");
    return EXIT_SUCCESS;
}


This post has been edited by PennyBoki: 22 Jan, 2008 - 08:45 PM
User is offlineProfile CardPM
+Quote Post

bector
RE: Array Problem
23 Jan, 2008 - 03:58 AM
Post #5

New D.I.C Head
*

Joined: 23 Jan, 2008
Posts: 8

your program will print duplicate values


i am try to solve your problem as soon as possible i given to detail of code


for any type of web related information you can visit

*mod edit - URL removed


QUOTE(bector @ 23 Jan, 2008 - 04:57 AM) *

your program will print duplicate values


i am try to sole your problem as soon as possible i given to detail of code


for any type of web related information you can vist

*mod edit - ditto


This post has been edited by jjhaag: 23 Jan, 2008 - 10:29 AM
User is offlineProfile CardPM
+Quote Post

Nayana
RE: Array Problem
23 Jan, 2008 - 05:04 AM
Post #6

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
bector is spamming!! Delete all his posts (please)... all of them are completely useless answers with a link to his stupid company.
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Array Problem
23 Jan, 2008 - 10:49 AM
Post #7

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
QUOTE(bector @ 23 Jan, 2008 - 04:58 AM) *

your program will print duplicate values


i am try to solve your problem as soon as possible i given to detail of code


for any type of web related information you can visit

*mod edit - URL removed


QUOTE(bector @ 23 Jan, 2008 - 04:57 AM) *

your program will print duplicate values


i am try to sole your problem as soon as possible i given to detail of code


for any type of web related information you can vist

*mod edit - ditto


No, my program will not print the duplicates. It could have its own errors but it will not print the duplicate values.

This post has been edited by PennyBoki: 23 Jan, 2008 - 10:50 AM
User is offlineProfile CardPM
+Quote Post

captainhampton
RE: Array Problem
23 Jan, 2008 - 11:09 AM
Post #8

Jawsome++;
Group Icon

Joined: 17 Oct, 2007
Posts: 518



Thanked: 2 times
Dream Kudos: 825
My Contributions
QUOTE

No, my program will not print the duplicates. It could have its own errors but it will not print the duplicate values.


Nope I checked and complied does not print duplicates, works just fine. Thanks again for all of your help I really do appreciate it.

This post has been edited by captainhampton: 23 Jan, 2008 - 11:10 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 11:32AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month