Click here to Skip to main content
15,879,068 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
hello!
i want to ask a question about c programming
i want a c program of array that reads 5 numbers each of which is between 10 and 100 ,as each number is read print it only if it is not a duplicate of a number already read .
i made one but it is not working perfectly :
can you tell me what is the wrong of my program :

What I have tried:

C
#include<stdio.h>
int main()
{
    int a[50];
    int i,j,k;
    for(i=0;i<5;i++){
        scanf("%d",&a[i]);
        while(a[i]<=10||a[i]>=100)
            scanf("%d",&a[i]);
        for(k=0;k<4;k++)
            for(j=k+1;j<5;j++)
            if(a[k]==a[j])
            scanf("%d",&a[i]);
    }
}
Posted
Updated 5-Jun-16 3:52am
v3
Comments
Sergey Alexandrovich Kryukov 4-Jun-16 15:33pm    
Please click "Improve question" and see how the code should be formatted — I formatted it for you, but it's not all; you also need to indent lines properly. Note that <stdio.h> was not shown, because you did not escape HTML characters...
—SA

There are many wrong things: hard-code immediate constants, such a 4, 5, 50, 100, pointless array of 50 elements (why 50?) lack of any output, not even any prompt for the user, no functions except entry point… Essentially, the code does nothing…

The notes unrelated to the code semantics: don't use so short names, indent lines properly. Create and use some functions.

In case of slightest concerns of you runtime, always use the debugger; always use it before asking such questions.

—SA
 
Share this answer
 
v4
Neglecting user presentation issues which you have done zero of lets look at the code you have given us
scanf("%d",&a[i]);
while(a[i]<=10||a[i]>=100)
  scanf("%d",&a[i]);

Will accept the first entry whether or not it is valid, then check for validity on the next line.
while(a[i]<=10||a[i]>=100)
  scanf("%d",&a[i]);

Those lines are correct and all that is needed as it does the required check.

Then all you need to do is check if the number already exists .. you just got entry a[i].
The valid prior entries are in array a[0]... a[i-1]

So all you need to do is check if a[i] matches any entry in a[0] .. a[i-1] ... if it does don't accept it.

You go off into lala land with j and k loops .. HINT you need only 1 loop which goes from a[0] to a[i-1] read sentence above.
Hint you can simply undo the increment of i, if you need to dump the last value.
Final hint you need only two loops and 1 if statement besides your check statement above to do the job efficiently.

Finish the code then look at presentation and improvements and removing unnecessary stuff.

SOLUTION AS YOU GOT STUCK:
C#
int a[5];
int i;
// For each of the required 5 numbers
for (i = 0; i < 5; i++){
    // The check .. Must be first a valid from Scanf (you forgot that) ...and then must be not less that 10 .. not greater than 100
    // READ Scanf SPECS ... On success, the function returns the number of items filled ... SO YOU WANT 1.
    // Blind loop until all three criteria met ,, scanf returns 1 and number between 10-100
    while ( (scanf("%d", &a[i]) != 1) || (a[i] <= 10) || (a[i] >= 100) ) {};

    // Check previous entries since we have a valid one
    // If you look i = 0 (first number) will not enter loop as j can not be less than i
    int j;
    for (j = 0; j < i; j++){
        // look for a match a[i] == a[j]
        if ((a[j]) == (a[i])){
            // You have a match so the current number is invalid IT EXISTS ALREADY
            // So decrement i so the increment is undone
            i--;
            // then break out of the j loop
            break;
        }
    }
}
 
Share this answer
 
v16
Comments
Member 12565627 5-Jun-16 10:06am    
please tell me how i've tried many times to make it but it doesn't working
how to make the program compare between a[0] and a[1] // a[1] and a[2] and so on
make it for me please !
leon de boer 5-Jun-16 10:28am    
I put the solution at the end since you got stuck and are losing heart :-)

There are various ways to do the loops but that is basics and it is important you understand the comments, so you get what you should have done.
Member 12565627 5-Jun-16 11:00am    
thank you very much i want to ask another question please help me i have final exam in the university in c programming
i want a program to reverse words in a string but not characters :
(birds and bees) will be (bees and birds)
thank you for helping
leon de boer 5-Jun-16 11:29am    
Look at the function strchr and use it find a whitespace character. When you have them all write the string back out starting from last one first.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900