Chat LIVE With Programming Experts! There Are 23 Online Right Now...

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

Join 244,184 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,339 people online right now. Registration is fast and FREE... Join Now!




logic problem

 
Reply to this topicStart new topic

logic problem, no duplicates in input

progstud
8 Jan, 2009 - 02:12 AM
Post #1

New D.I.C Head
*

Joined: 21 Nov, 2008
Posts: 36

Hi guys..back again...I was confused if i should start another topic...anyways.. I am working a problem at the moment that ask the user to enter 10 integers greater than 10. Also the number should be printed only if it is not a duplicate(already entered). Really my problem is more about logic at the moment..
CODE
#include<stdio.h>

int main(void)
{
    int array[5];
    int i;
    int number;
    int j;
    
    for(i=0;i<5;i++)
    {
        do
        {
        printf("Enter a number..not duplicate and greater than 10: ");
        scanf("%d",&number);
        
        }while(number<=10 );
        
        array[i]=number;
        
    }
    
    /* printing array */
    for(i=0;i<5;i++)
    {
        printf("a[%d] %d",i,array[i]);
        printf("\n");
    }
}


User is offlineProfile CardPM
+Quote Post


Stofke
RE: Logic Problem
8 Jan, 2009 - 04:40 AM
Post #2

New D.I.C Head
*

Joined: 7 Jan, 2009
Posts: 42



Thanked: 1 times
My Contributions
Should it be something like that?

CODE

#include <stdio.h>
#include <stdlib.h>

int main()
{
     int array[5];
     int i,j,number;
    for (i=0;i<5;i++)
    {
REDO:
        printf("Enter a number..not duplicate and greater than 10: ");
        scanf("%d",&number);
        if (number <10){
        array[i]=number;
        }

        for ( j = 0; j < i; ++j )
        {
            if ( array[i] == array[j] )
            {
                printf("already in the array ! try again \n");
                goto REDO;
            }
        }
    }

    /* printing array */
    for(i=0;i<5;i++)
    {
        printf("a[%d] %d",i,array[i]);
        printf("\n");
    }
}


User is offlineProfile CardPM
+Quote Post

Gloin
RE: Logic Problem
8 Jan, 2009 - 05:18 AM
Post #3

Expert Schmexpert...
Group Icon

Joined: 4 Aug, 2008
Posts: 2,508



Thanked: 128 times
Dream Kudos: 75
My Contributions
Unless you wanna switch to using some more suitable datastructure than arrays, what you need to do is.

* Loop
* Ask for a number
* See if it's already in the array
* If it isn't, put it into the array and update the array size
* Stop when the array size equals 10

The only reason I'm making this post is because I see absolutely no reason to use GOTO statements here.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 7/4/09 06:14AM

Live C++ Help!

Be Social

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

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month