Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Duplicate Input

jcytnjcytn Member Posts: 3
I m trying to run a program to show arraySize[20] of numbers 10 to 100 inclusive. Numbers not within range r 'Invalid' - hv got it. But I can't get seem to get the formula in order to output 'Duplicate numbers' (as each number is read, print it only if it is not a duplicate of a number already read). Meaning the output of the 20 numbers will be different. Below r the what I have done.
Somebody out there, help me?

#include

int linearSearch (const int [], int, int);

int main()

{
const int arraySize = 20;
int num[arraySize], searchKey, ;

for (int i=0; i<=arraySize; i++) {
num[i] = 10 + i;

cout << "Enter a number between 10 & 100 inclusive : ";
cin >> searchKey;

if (searchKey >= 10 && searchKey <= 100);
else
cout << "Invalid number!!! Choose another
number : " << endl;

//(wrong...) if (searchKey == num[arraySize)
// cout << "Duplicate number!!!" << endl;
}

return 0;

}


Comments

  • Geeth AsokanGeeth Asokan Member Posts: 127
    : I m trying to run a program to show arraySize[20] of numbers 10 to 100 inclusive. Numbers not within range r 'Invalid' - hv got it. But I can't get seem to get the formula in order to output 'Duplicate numbers' (as each number is read, print it only if it is not a duplicate of a number already read). Meaning the output of the 20 numbers will be different. Below r the what I have done.
    : Somebody out there, help me?
    :
    : #include
    :
    : int linearSearch (const int [], int, int);
    :
    : int main()
    :
    : {
    : const int arraySize = 20;
    : int num[arraySize], searchKey, ;
    :
    : for (int i=0; i<=arraySize; i++) {
    : num[i] = 10 + i;
    :
    : cout << "Enter a number between 10 & 100 inclusive : ";
    : cin >> searchKey;
    :
    : if (searchKey >= 10 && searchKey <= 100);
    : else
    : cout << "Invalid number!!! Choose another
    : number : " << endl;
    :
    : //(wrong...) if (searchKey == num[arraySize)
    : // cout << "Duplicate number!!!" << endl;
    : }
    :
    : return 0;
    :
    : }
    :
    :
    :
    Your program is incorrect as some of your variables are not declared at the beginning of your program.

    Now, to enter in the numbers within a specified range, try this:

    [code]
    #include <iostream>
    using namespace std;

    int main()
    {
    // Declaring the variables
    int arraySize = 20;
    int num[arraySize];
    int i, j;
    int searchKey[arraySize];


    // Beginning of 'for' loop
    for(i=0; i < 11; i++)
    {
    // User enters 10 numbers within the specified range
    cout << "Enter 10 number between 10 & 100 inclusive: ";
    cin >> searchKey[i];

    if(searchKey[i] >= 10 && searchKey[i] <= 100)
    {
    // Print out the number
    cout << searchKey[i] << "
    ";
    }
    else
    {
    cout << "Invalid number!!!
    ";
    cout << "
    Choose another number:
    ";
    }
    // End of 'for' loop
    }

    return 0;
    }
    [/code]

    and in order to check for duplicate numbers, you may need to use an [b]if - else[/b] statement.

  • Shawn CarterShawn Carter Member Posts: 0

    ____ ( http://forcoder.org ) free ebooks and video tutorials about { PL/SQL Python MATLAB C C# R Visual Basic .NET JavaScript PHP Go Ruby Objective-C Assembly Visual Basic Delphi Swift Scratch Java Perl C++ Dart Crystal Bash Erlang Rust COBOL Alice Clojure Awk F# FoxPro Fortran D Prolog Lisp Julia ML Scala Logo Scheme Hack Apex Transact-SQL VBScript LabVIEW ABAP SAS Kotlin Lua Ada } _

Sign In or Register to comment.