General idea help (Homework)

This is a discussion on General idea help (Homework) within the C Programming forums, part of the General Programming Boards category; Hey, I have a homework question that I've tried to complete but I hit a wall. The objective is to ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    14

    General idea help (Homework)

    Hey, I have a homework question that I've tried to complete but I hit a wall. The objective is to have a user input some numbers so that the program can output two columns, one showing each unique number that the user inputted, and the other showing how many times the user inputted said number.

    example.(I don't know the formatting of this thread)

    Input: 1 5 3 5 5 12 3
    Unique # frequency
    1 1
    5 3
    3 2
    12 1


    I have no idea on how to approach this. I'm super beginner at C and all i've managed to come up with is:
    Code:
    #include <stdio.h>
    #define list 50
    #define value 50
    
    
    int check (int x,int num,int amt);
    
    
    int main(){
        int x[value],num,i,j,k,amt;
        
        printf("Enter the amount of numbers:");
        scanf("%d",&amt);
        
        for (i=1;i<=amt;i=i+1){
        printf("Enter Number[%d]:  ",i);
        scanf("%d",&num,&x[i]);
        
        check(x,num,amt);
    
    
        return 0;
    }
    
    
        int check (int x,int num){
            int j,k,y
    [list]
    
    
            for (j=1;j<amt;j=j+1){
                if(x[j]=num)
    As much as I'd like someone to do the whole thing for me, that's not gonna happen. I would like some advice as to whether i'm on the right track. I think if I use some crazy continue statements, I might be able to do it, but I've hit a wall. If someone would be so kind as to offer suggestions on routes I can take, I'd greatly appreciate it (Although it might be a long shot since I pretty much have nothing so far). Thanks for your time!

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    there are a couple of ways to do this.
    1. if you are sure the numbers are within a certain range, you can allocate an array of ints of size equal to the range of numbers. each element in the array represents the count of occurrences of the index to the array. for example, the users enters the value 12. you increment array[12]. then at the end you iterate through the array and print the counts that are greater than 0. this works as long as the range of input numbers isn't too big. although on a modern PC an array of 1000000 will work fine.
    2. if that doesn't work, then you need to keep an association between the numbers entered and the count. one way to do this is to have a an array of struct with 2 members, the value entered and the count for that value. when a number is entered, you search the array to see if it has already been entered. if it has, you increment the count for that value. if not, you add it to the next empty location in the array. keep a count of how many elements have been entered. if you don't like structs, you can use two parallel arrays where one is the value and one is the count for that value. just use the same index to access each.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There is an association between the index of the array[index], and the number itself. That's a real strong association that is used many times in solving problems with arrays. (this is #1 way, listed above).

    Consider this:
    Code:
    int array[10]={7,7,7,7,1,2,3,8,9,4};
    int count[10]=[0];
    
    for(int i=0;i<10;i++)
       count[array[i]]++;
    When you print out the count array, what do you have? All possible because you were able to associate the value of the array, with the index of count[]. It's a subtle, but powerful tool.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2,706
    You've received good advice on solving your problem thus far - if the proposed solutions don't meet your needs, be sure to let us know.

    It's difficult to comment on your current solution, since your "check()" function seems to have been cut short.

    Some observations of your code:

    Code:
    for (i=1;i<=amt;i=i+1)
    Array indices start at zero and go to 'n-1'. But your "for()" loops start at one and go to 'n' - this may cause a buffer overrun (if the user enters the full amount of numbers; 50, in this case). If you did this so that your print statements start at one and not zero, just do a simple addition in the "printf()" argument:

    Code:
    for (i=0;i<amt;i=i+1)
        printf("Enter Number %d:  ",i+1);
    Note that you have the same issue in the other "for()" loop (line 31).

    --------

    Code:
    scanf("%d",&num,&x[i]);
    You're scanning one integer, but providing two arguments. Why not just read the integer into 'num' and then assign the value of 'num' to 'x[i]' in a separate instruction?

    --------

    Code:
    if(x[j]=num)
    Be careful - this is a common mistake and is not always easy for a beginner to track down. You want to use the '==' operator here, not the '=' operator.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    14
    Hey, sorry I haven't responded back, had to run some errands. In response to dmh2000, the first option unfortunately can't be used because the user can input whatever number they please. I haven't really learned anything about parallel arrays, so I've been trying to figure that out. For Adak, I'm kind of confused on what the
    Code:
    count[array[i]]++;
    part does. Does it somehow store the counts for the unique values? How does it do that? Sorry guys, I have a really really basic understanding about these things so I'm kind of lost here. As for Matticus, thanks for the syntax checking. That stuff will be the bane of me. I'll just make it easier and start at 0 for the increments. As of right now, my code hasn't really gone anywhere haha. Checking up on parallel arrays and how they work. Thanks for the responses so far though guys!

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    14
    Update here. I've tried to use three arrays to make some kind of sorting machine and I feel like I'm close-ish. There are some major errors though, and I can't find the logic since it's kind of a mess.

    Code:
    #include <stdio.h>
    #define count 50
    #define value 50
    #define pool 50
    
    
    int main(){
    	int x[pool];
    	int y[value];
    	int z[count];
    	int j,k,i,n,amt;
    
    
    	printf("Enter the amount of numbers:  ");
    	scanf("%d",&amt);
    	
    	for (i=0;i<=amt;i=i+1){
    	printf("Enter Number[%d]:  ",i);
    	scanf("%d",&x[i]);
    	}
    	
    	for (j=0;j<=amt;j=j+1){
    			for (k=0;k<=amt;k=k+1){
    				if(y[k]==x[j]){
    					z[k]+1;
    				}
    				else{
    					y[k]=x[j];
    				}
    			}
    	}
    	printf("\n\nUnique #    Frequency");
    	printf("\n-------    ---------");
    	for (n=0;n<=amt;n=n+1){
    		printf("  %d        %d",y[n],z[n]);
    	}
    	return 0;
    }
    Suggestions haha?

  7. #7
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    To understand an expression like this, you can just use a substitution model. Start with the innermost expression and evaluate it. So 'i' is an integer that is incrementing from 0 to 9 (when it is 10 the loop ends), then the first iteration i = 0. Then evaluate array[0] = 7 in Adak's example. Then evaluate count[7] which we'll presume is 0. Then count[7] = 0 + 1 by the ++ operator. (Remember that ++ means 'lvalue = rvalue + 1', otherwise you might try 0++ which is meaningless). Then the loop continues and i = 1, and so on in the same fashion. So you can see that array[i] is being evaluated for an integer value that is then used as the index for count.

    Of course, if a user can enter any valid int value, then you'd need an array of 2^32 elements in most cases. Your choice really depends on the spec your instructor gave you. If you can limit the range, then this approach is great.

    Parallel arrays is just a general beginning concept for binding data. Eg. array1[0]'s data is associated with array2[0]'s data, array1[1]'s with array2[1], and so on.

  8. #8
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    A couple things...

    You create arrays x, y of size 50, but then you use the user entered value amt to control your indexing of those arrays in your loops. This becomes a problem if the user enters amt > 50.
    So you need to validate the user's amt as being less than or equal to 50. (Or alternatively, dynamically allocate the array according to amt).

    Let's say amt == 50. Then your for loops stop when i or j is > 50. Given an array of 50 elements, the valid indices for the array are 0 to 49, or more generally, 0 to size-1. Just keep that in mind when writing your 'test' expression in the for() loop or you will try to access an invalid array element.

    Next, z[k]+1 is an expression that has no side-effect. It will simply evaluate the sum of z[k]'s value and 1. It will not change anything. z[k]++ is probably what you're looking for. But, even this is a problem because you have no idea what values are initially in z.

    I think you're on the right track - sorting the user's values will get you on your goal. The logic won't be found in your code though - sit down on paper and think about how you would use the sorted values to build 2 arrays, one for the values and one for the count, in parallel. You'll have to think about the decisions needed for when to copy the value to values array and when not to, when to advance in either array and when not to, what action to take and how to test for duplicate user values, etc. Figure that out, and you've got your logic.
    Last edited by Tclausex; 02-14-2013 at 02:21 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [General Help] Dynamic Arrays and General Pointer syntax
    By Amberlampz in forum C Programming
    Replies: 22
    Last Post: 07-26-2011, 11:58 PM
  2. general b-tree idea
    By Cpro in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 01:13 PM
  3. C in general
    By majoub in forum C Programming
    Replies: 3
    Last Post: 02-17-2005, 02:06 PM
  4. General C++ Question
    By IncarnateRW in forum C++ Programming
    Replies: 2
    Last Post: 03-04-2003, 08:59 AM
  5. Good idea, bad idea.
    By sean in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 06-15-2002, 01:26 PM

Tags for this Thread


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