Thread: array search

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    72

    array search

    hi. i was looking through the "help" in C++ and found a search and attempted to mod it for searching through arrays. Unfortunately, it was unsuccessful.

    say i have the an array of name[5] of:

    1. KARL,GEORGE
    2. SMITH,JOE
    3. DEEN,LOW
    4. SUPER,MAN
    5. SPIDER,MAN

    how would i be able to search through the array for the first letter.

    User inputs:
    Code:
    cin >> start_with;
    if inputted:

    S

    then will display SUPER,MAN and SPIDER,MAN


    Note: need examples which are compatible with <cstring.h>

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    While the cin was nice, where is the rest of your code?

    I'll give you a hint:
    Code:
    loop
      if(expression is true)
        print
    I know my code pales in comparison to your cin, but I hope it helps.

  3. #3
    Registered User dizolve's Avatar
    Join Date
    Dec 2002
    Posts
    68
    I'll go a little further, if you have an array of name[5] and element #0 is KARL,GEORGE then name[0][0] will return K. :]

    &nbsp;&nbsp;__&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;___&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;/\&nbsp;\&nbsp;&nbsp;__&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;/\_&nbsp;\&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;\_\&nbsp;\/\_\&nbsp;&nbsp;____&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_ __\//\&nbsp;\&nbsp;&nbsp;&nbsp;&nbsp;__&nbsp;&nbsp;__&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;__&nbsp;&nbsp;&nbsp;
    &nbsp;/'_`&nbsp;\/\&nbsp;\/\_&nbsp;,`\&nbsp;&nbsp;/&nbsp;__`\\&nbsp;\&nbsp;\&nbsp;&nbsp;/\&nbsp;\/\&nbsp;\&nbsp;&nbsp;/'__`\&nbsp;
    /\&nbsp;\_\&nbsp;\&nbsp;\&nbsp;\/_/&nbsp;&nbsp;/_/\&nbsp;\_\&nbsp;\\_\&nbsp;\_\&nbsp;\&nbsp;\_/&nbsp;|/\&nbsp;&nbsp;__/&nbsp;
    \&nbsp;\___,_\&nbsp;\_\/\____\&nbsp;\____//\____\\&nbsp;\___/&nbsp;\&nbsp;\____\
    &nbsp;\/__,_&nbsp;/\/_/\/____/\/___/&nbsp;\/____/&nbsp;\/__/&nbsp;&nbsp;&nbsp;\/____/
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;I&nbsp;have&nbsp;a&nbsp;BAD&nbsp;figlet& nbsp;addiction.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    72
    i dont know how to compare the first character with the inputted value.

    Code:
    for(int x = 1; x <= 5;x++)
    {
    
    if(start_with == name[x][0])
    {
    
        cout << name[x];
    }
    
    }
    i dont think this is right, and im pretty sure that will not work using cstring.h

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    i think what u need to do is

    for(int c=0;c<user_input;c++)
    {
    cout<<name[c];
    }

    note that this only shows how u could do so
    u need to change it so it suits your needs

  6. #6
    Registered User dizolve's Avatar
    Join Date
    Dec 2002
    Posts
    68
    Code:
    #include <stdlib.h>
    #include <ctype.h>
    #include <iostream>
    
    using namespace std;
    
    int main(void) {
      char start_with;
    
      cin >> start_with;
    
      for(int x = 0; x < 5;x++) {
        if(tolower(start_with) == tolower(name[x][0])) {
          cout << name[x] << endl;
        }
      }
    
      return 0;
    }
    I bet this is close to what you already have.
    Last edited by dizolve; 01-10-2003 at 09:29 PM.

    &nbsp;&nbsp;__&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;___&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;/\&nbsp;\&nbsp;&nbsp;__&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;/\_&nbsp;\&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;\_\&nbsp;\/\_\&nbsp;&nbsp;____&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_ __\//\&nbsp;\&nbsp;&nbsp;&nbsp;&nbsp;__&nbsp;&nbsp;__&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;__&nbsp;&nbsp;&nbsp;
    &nbsp;/'_`&nbsp;\/\&nbsp;\/\_&nbsp;,`\&nbsp;&nbsp;/&nbsp;__`\\&nbsp;\&nbsp;\&nbsp;&nbsp;/\&nbsp;\/\&nbsp;\&nbsp;&nbsp;/'__`\&nbsp;
    /\&nbsp;\_\&nbsp;\&nbsp;\&nbsp;\/_/&nbsp;&nbsp;/_/\&nbsp;\_\&nbsp;\\_\&nbsp;\_\&nbsp;\&nbsp;\_/&nbsp;|/\&nbsp;&nbsp;__/&nbsp;
    \&nbsp;\___,_\&nbsp;\_\/\____\&nbsp;\____//\____\\&nbsp;\___/&nbsp;\&nbsp;\____\
    &nbsp;\/__,_&nbsp;/\/_/\/____/\/___/&nbsp;\/____/&nbsp;\/__/&nbsp;&nbsp;&nbsp;\/____/
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;I&nbsp;have&nbsp;a&nbsp;BAD&nbsp;figlet& nbsp;addiction.

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    79
    Try this...
    Code:
    /*Compiler : Turbo C++ v3.01*/
    #include<iostream.h>
    #include<conio.h>
    #include<stdio.h>
    #define MAX 50
    int main()
    {
       char array[5][MAX],character;
       int ctr;
       for(ctr=0; ctr<5; ++ctr)
          gets(array[ctr]);
       clrscr();
       cout<<"Enter the character:"; cin>>character;
       for(ctr=0; ctr<5; ++ctr)
       {
          if(array[ctr][0]==character)
             cout<<array[ctr];
       }
       cout<<endl<<"Press a key..."; getch(); return(0);
    }

  8. #8
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953

    Re: array search

    Originally posted by niroopan
    hi. i was looking through the "help" in C++ and found a search and attempted to mod it for searching through arrays. Unfortunately, it was unsuccessful.

    say i have the an array of name[5] of:

    1. KARL,GEORGE
    2. SMITH,JOE
    3. DEEN,LOW
    4. SUPER,MAN
    5. SPIDER,MAN

    how would i be able to search through the array for the first letter.

    User inputs:
    Code:
    cin >> start_with;
    if inputted:

    S

    then will display SUPER,MAN and SPIDER,MAN


    Note: need examples which are compatible with <cstring.h>
    If you have a single subscripted array of chars called array, you can access the first element like this:
    Code:
    //the char array
    char array[5];
    //you access like this:
    array[0];
    If you have a double subscripted array and you want to access the first element in each row you do this:
    Code:
    char array[5][5];
    for ( int i=0; i<5; i++ )
         //this is how you access the first element in each row:
         array[i][0];
    And you can comopare the characters like this:
    Code:
    if ( array[0] == 'a' )     //returns true if the first char in the array is a
    Hope this is what you are asking for...

    Note: You don't need to use the string library <cstring> to do the above, and there is no <cstring.h> there is the old <string.h> and the new <cstring>
    none...

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    72
    this code will only work for arrays declared as char]

    Code:
    char name[25][25];
    i need the code for the declaration:

    Code:
    string name[25];
    the 'string' class is found in the cstring.h library. That is why im having trouble. I need to search through an array which has the declaration of an array using the declaration type of 'string' instead of the type 'char'.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. search array program
    By z.tron in forum C++ Programming
    Replies: 3
    Last Post: 11-15-2002, 07:33 AM
  5. binary search in an array
    By brianptodd in forum C++ Programming
    Replies: 4
    Last Post: 11-12-2002, 02:05 PM