A few problems getting data from an array

This is a discussion on A few problems getting data from an array within the C Programming forums, part of the General Programming Boards category; Hey guys, having a bit of trouble with more of this array stuff. This is my code so far: Code: ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    27

    A few problems getting data from an array

    Hey guys, having a bit of trouble with more of this array stuff. This is my code so far:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    
    {
    int data[50];
    int i, n, start, stop;
    
    printf("How many integers should I read (1--50)? ");
    scanf("%d", &n);
    
    if(n > 50)
    {
    	printf("That value is too large.\n");
    		exit(1); 
    }
    
    printf("\nEnter your values, one per line.\n");
    
    for(i = 0; i < n; i++)
    	scanf("%d", &data[i]);
    
    printf("\nYou entered\n");
    	for(i = 0; i < n; i++)
    	printf("%d\n", data[i]);
    
    	printf("Enter a Starting Value: ");
    	scanf("%d", &start);
    
    	printf("Enter a Stopping Value: ");
    	scanf("%d", &stop);
    
    	
    }
    There are a few things I gotta get out of this. I have to print the values from the array that are greater than or equal to the starting value AND less than or equal to the stopping value. I then have to count the number of values between the starting and stopping values and how many of them are even and how many are odd. I tried using some IF statements, but it wasnt working out.

  2. #2
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64

    Thumbs up solution

    Try with the following code,

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    
    {
    int data[50];
    int i, n, start, stop;
    int count=0,even=0,odd=0;
    printf("How many integers should I read (1--50)? ");
    scanf("%d", &n);
    
    if(n > 50)
    {
            printf("That value is too large.\n");
                    exit(1);
    }
    
    printf("\nEnter your values, one per line.\n");
    
    for(i = 0; i < n; i++)
            scanf("%d", &data[i]);
    
    printf("\nYou entered\n");
            for(i = 0; i < n; i++)
            printf("%d\n", data[i]);
    
            printf("Enter a Starting Value: ");
            scanf("%d", &start);
    
            printf("Enter a Stopping Value: ");
            scanf("%d", &stop);
    
            printf("Elements are :");
    for(i=0;i<n;i++)
    {
            if(data[i]>=start&&data[i]<=stop)
            {
                    printf("%d ",data[i]);
                    count++;
                    if((data[i]%2)==0)
                            even++;
                    else
                            odd++;
            }
    
    }
    
    printf("\n");
    printf("Count : %d \n",count );
    printf("Odd : %d \n", odd );
    printf("Even : %d \n",even );
    }

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    27
    Quote Originally Posted by karthigayan View Post
    Try with the following code,

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    
    {
    int data[50];
    int i, n, start, stop;
    int count=0,even=0,odd=0;
    printf("How many integers should I read (1--50)? ");
    scanf("%d", &n);
    
    if(n > 50)
    {
            printf("That value is too large.\n");
                    exit(1);
    }
    
    printf("\nEnter your values, one per line.\n");
    
    for(i = 0; i < n; i++)
            scanf("%d", &data[i]);
    
    printf("\nYou entered\n");
            for(i = 0; i < n; i++)
            printf("%d\n", data[i]);
    
            printf("Enter a Starting Value: ");
            scanf("%d", &start);
    
            printf("Enter a Stopping Value: ");
            scanf("%d", &stop);
    
            printf("Elements are :");
    for(i=0;i<n;i++)
    {
            if(data[i]>=start&&data[i]<=stop)
            {
                    printf("%d ",data[i]);
                    count++;
                    if((data[i]%2)==0)
                            even++;
                    else
                            odd++;
            }
    
    }
    
    printf("\n");
    printf("Count : %d \n",count );
    printf("Odd : %d \n", odd );
    printf("Even : %d \n",even );
    }
    Thanks a lot man. Got an error about the count, even, and odd not being declared or something, but I figured that out.

    Question, the data[i] is basically the directory for everything that was inputted into the array? I thought you would have to do a separate statement for each variable in the array.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    15
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    
    {
    int data[50];
    int i, n, start, stop;
    
    printf("How many integers should I read (1--50)? ");
    scanf("%d", &n);
    
    if(n > 50)
    {
            printf("That value is too large.\n");
                    exit(1);
    }
    
    printf("\nEnter your values, one per line.\n");
    
    for(i = 0; i < n; i++)
    scanf("%d", &data[i]);
    
    int j ;
    int temp;
    int stat,stat1;
    
    for(j=0;j<n;j++)
    {
    for(i=j;i<n-1;i++)
    if(data[j]>data[i+1])
    {
    temp=data[j];
    data[j]=data[i+1];
    data[i+1]=temp;
    }
    }
    
     printf("\nYou entered\n");
    for(i = 0; i < n; i++)
    printf("%d\n", data[i]);
    
            printf("Enter a Starting Value: ");
            scanf("%d", &start);
    
      printf("Enter a Stopping  Value: ");
            scanf("%d", &stop);
    
     for(i=0;i<n;i++)
    {
            if(data[i] == start)
            {
               stat=i;
            }
            if(data[i] == stop)
            {
                    stat1=i;
            }
    }
    
    int even=0;
    int odd=0;
    int count=0;
     printf("The  elements between %d and %d are\n",start,stop);
    for(i=stat;i<=stat1;i++)
    {
             count++;
            printf("%d ", data[i]);
            if((data[i]%2)==0)
                   even++;
            else
                    odd++;
    }
    printf("\nThe odd is :%d\n",odd);
    printf("The even is :%d\n",even);
    printf("The count is:%d\n",count);
    }
    Note: This code will take the start and stop number as a last number only, if you give the duplicate element for an array values.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64

    Exclamation

    sorry ! I can not understand your question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of strings problems...
    By chubigans in forum C Programming
    Replies: 4
    Last Post: 08-27-2009, 04:13 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  3. How to complete program. Any probs now?
    By stehigs321 in forum C Programming
    Replies: 7
    Last Post: 11-19-2003, 04:03 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  5. Array sorting problems
    By cazil in forum C++ Programming
    Replies: 2
    Last Post: 02-04-2002, 01:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21