Thread: looking inside of an array

  1. #1
    In The Light
    Join Date
    Oct 2001
    Posts
    598

    looking inside of an array

    hello,
    this code will compare a single array location with the value returned from the fun() function.
    i would like to compare the fun() value to all of the locations in the array.
    any ideas?

    ~~~~~~~~~~~~~~~~~~~~~~~code~~~~~~~~~~~~~~~~~

    /*code to test the access of data from text files and DB conversion*/
    /*files*/

    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <stdio.h>
    int fun(void);

    void db_access(void)
    {

    system ("clear");
    cout<<"Start of Database\n\n";
    char ch;
    int a[12];
    int k;
    int i;

    for (i=0; i<12; i++) a[i] = i;

    cout<<"\n\nEnd of Database\n";
    printf("XXXXXX: %i\n", a[0]);
    printf("XXXXXX: %i\n", a[1]);
    printf("XXXXXX: %i\n", a[2]);
    printf("XXXXXX: %i\n", a[3]);
    printf("XXXXXX: %i\n", a[4]);
    printf("XXXXXX: %i\n", a[5]);
    printf("XXXXXX: %i\n", a[6]);
    printf("XXXXXX: %i\n", a[7]);
    printf("XXXXXX: %i\n", a[8]);
    printf("XXXXXX: %i\n", a[9]);
    printf("XXXXXX: %i\n", a[10]);
    printf("XXXXXX: %i\n", a[11]);

    k = fun();
    cout<<fun()<<"\n";
    cout<<k<<"\n";
    if(a[8]<k)cout<<"Sucess\n";

    cout<<"Press any key to continue.\n";
    cin.get();
    char x;


    }

    int fun()
    {
    int k;
    k=9+4;
    return k;
    }

    int main()
    {
    system("clear");
    fun();
    db_access();
    return 0;
    }


    ~~~~~~~~~~~~~~~~~~~~code~~~~~~~~~~~~~~~~~~~~~

    thanks
    M.R.

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    Use a for loop to compare each value of the array the the value returned by fun()

    Code:
    int target = fun();
    for( int i=0 ; i< 12 ; i++)
    {
      if( array[i] == target )
      {
         // do sumtin
       }
       else
        {
            //do sumtin else
        }
    }  // end for loop
    and you can use the CODE and /CODE tags, inside [] to post code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. Realloc problems with sturcture array inside structure
    By daveyand in forum C Programming
    Replies: 2
    Last Post: 03-29-2004, 06:48 AM