Thread: arrays with elements

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45

    arrays with elements

    Hello everyone,
    I am having the user input a integer. Then from that input, search an array with static elements. Then return(print) how many times the users int was found in array. I am using a for loop to iterate through the array, but can not get it to count how many times it appears. This is the rough version I have so far:
    Code:
    #include <stdio.h>
    
    
    #define SIZE 20
    
    int n[]={25,68,15,35,84,56,8,52,27,7,53,15,8,42,32,1,5,8,41,23};
    
    int c;
    int t;
    
    int main(void)
    {
            printf("enter numer to search?");
            scanf("%d",&t);
            for (c=0;c<SIZE;c++) {
                if (t==n[c])
                   printf ("yes");
                else {
                    printf ("no");
               }
            printf ("the value %d was found %d times in array",t,n[c]);
            }
    
    
    
    }
    I know the last printf is wrong, but I am running out of ideas.
    Thanks in advance for any suggestions.
    Brad

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Create another variable called say
    int numberOfMatchesFound = 0;

    Which you increment at some key part in the code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by bradleyd View Post
    Hello everyone,
    I am having the user input a integer. Then from that input, search an array with static elements. Then return(print) how many times the users int was found in array. I am using a for loop to iterate through the array, but can not get it to count how many times it appears. This is the rough version I have so far:
    Code:
    #include <stdio.h>
    
    
    #define SIZE 20
    
    int n[]={25,68,15,35,84,56,8,52,27,7,53,15,8,42,32,1,5,8,41,23};
    
    int c;
    int t;
    int count = 0;
    
    int main(void)
    {
            printf("enter numer to search?");
            scanf("%d",&t);
            for (c=0;c<SIZE;c++) {
                if (t==n[c]) {
                   printf ("yes");
                   count++;            
                }
                else {
                    printf ("no");
               }
            printf ("the value %d was found %d times in array", t, count);
            }
    
    
    
    }
    I know the last printf is wrong, but I am running out of ideas.
    Thanks in advance for any suggestions.
    Brad
    Try that.

    Adak

  4. #4
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    thanks Salem, and Adak. Should I move the printf statement out of the for loop? I keep getting this as ouput.
    enter numer to search?8
    nothe value 8 was found 0 times in arraynothe value 8 was found 0 times in arraynothe value 8 was found 0 times in arraynothe value 8 was found 0 times in arraynothe value 8 was found 0 times in arraynothe value 8 was found 0 times in arrayyesthe value 8 was found 1 times in arraynothe value 8 was found 1 times in arraynothe value 8 was found 1 times in arraynothe value 8 was found 1 times in arraynothe value 8 was found 1 times in arraynothe value 8 was found 1 times in arrayyesthe value 8 was found 2 times in arraynothe value 8 was found 2 times in arraynothe value 8 was found 2 times in arraynothe value 8 was found 2 times in arraynothe value 8 was found 2 times in arrayyesthe value 8 was found 3 times in arraynothe value 8 was found 3 times in arraynothe value 8 was found 3 times in ar

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    What do you think?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yepper!

    Adak

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-04-2008, 08:27 PM
  2. Comparing elements of character pointer arrays
    By axe in forum C Programming
    Replies: 2
    Last Post: 11-14-2007, 12:20 AM
  3. summing an array's elements
    By gomindi in forum C++ Programming
    Replies: 10
    Last Post: 11-21-2003, 04:37 PM
  4. Pointer of arrays - how do I access their elements?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-11-2001, 11:28 PM
  5. elements of arrays; functions
    By sballew in forum C Programming
    Replies: 6
    Last Post: 09-03-2001, 01:48 AM