anyone here who can help me on this?

I need to create a program that can output the location of 5 inputted values.
sample input/output;
enter 5 values: 4 5 9 8 3
enter no to search: 8
8 found in location 4

HOPE YOU GET IT> pls give me a program on this, i cant seem to make the program work with regards to the location thing.
tnx

BYE,
Claire

Recommended Answers

All 12 Replies

You seem to have not tried at all. Give it a shot and then if it doesn't work we'll help you.

commented: Quite so +29

Do something with arrays.
Create a array and input the 5 location in it. Then run a loop and check for the given value.
I mean, do SOMETHING man.
We don't give free candies!

Use argc and argv .

This is my program. Sorry for being a newbie. The location always display 1.

#include<stdio.h>
main()
{
int no[5];
int c,d,loc;
printf("\n Enter 5 values:");
for(c=0;c<5;c++)
{
scanf("%d",&no[c]);
}
printf("\n Enter a number to search:");
scanf("%d",&d);
loc=0;
for(c=0;c<5;c++)
{
if(d==no[c])
loc=loc+1;
}
printf("\n The location is: %d", loc);
getch();
}

The following code will do the job:

#include <iostream>

using namespace std;

int main(void)
{
        const int MAX_NUMS = 5;
        int value_array[MAX_NUMS] = {0}; // initialize to zero
        int value_pos_array[MAX_NUMS] = {0}; // initialize to zero
        int number_to_find;

        cout << "Enter " << MAX_NUMS << " values: ";

        for(int i = 0; i < MAX_NUMS; i++)
        {
            cin >> value_array[i];
        }

        cout << endl;

        /* Check user input */
        for(int i = 0; i < MAX_NUMS; i++)
        {
            if (value_array[i] == 0)
            {
                cout << "Not enough numbers entered !!!" << endl;
                exit(1);
            }
        }

        cout << "Enter a number to search: ";
        cin >> number_to_find;

        /* Count the position(s) of number_to_find */
        int count_match = 0;
        for(int i = 0; i < MAX_NUMS; i++)
        {
            if (value_array[i] == number_to_find)
            {
                value_pos_array[count_match] = i+1; // store the position of the number
                count_match++; // increment by one
            }
        }

        /* Display the result on the screen */
        if(count_match == 0)
        {
            // no numbers found
            cout << "There were no matching numbers !!!" << endl;
        } else {
            // matching numbers were found
            // display the positions of those numbers on the screen

            cout << "Found " << count_match << " match(es)" << endl;
            cout << "On the following positions: ";

            for(int i = 0; i < count_match; i++)
            {
                if (value_pos_array[i] != 0)
                {
                    cout << value_pos_array[i];
                    if(value_pos_array[i+1] != 0)
                    {
                        cout << ", ";
                    }
                }
            }
            cout << endl;
        }

        return 0;
}

Thanks to ArkM to help me fixing a bug in the program !!

question pls...
i'm having problems using the while and do while loop.

1.i=0;
2.    while(i<5){
3.    scanf("%d",&nos[i]);
4.      delay(0);
5.      i++;
6. }
7.     loc=0
8.     printf("\n Enter a number to search:");
9.     i=0;
10.    while(i<5){
11.    scanf("%d",&find);
12.      delay(0);
13.      i++;
14. }
15.  if(nos[i]==find)
16. printf("\n The location is: %d", loc);
17. loc=i+1;

It won't run the way it should.

tnx,
CLaire

take out the delay()s. They add nothing to the code and are distracting.

scanf("%d",&find); 
while(loc < 5)
{
   if(nos[loc] == find)
   {
      printf("\n The location is: %d", loc + 1);
   }
   ++loc;
}

For maximum learning benefit explain each change from my code to yours.
For better code writing, write out logic step by step by hand before writing code. Then write code to match each step of logic.

Why are you writing ancient C++ code?
It's much easier to use the new C++ standard ...

I think scanf is more C-like than C++-like I would rather recommend using cin and cout for in- and output ...

I'd be careful calling C code ancient. It's still used in many modern applications and is very much alive. Agreed posting C code on a C++ board may be a bit pretentious but many newbies haven't been told the difference yet.

Who said C is ancient?
He meant to say that the C++ you are using is ancient.
As most of people assume, C is a ancient C++, while it is not.
But the fact remains that DaniWeb has a C forum too, and this problem had been soughed more finely there.

Thanks I got it! You guys really are the best!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.