Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: Your insights needed!

  1. #1
    Join Date
    Jul 2002
    Posts
    1,395
    Rep Power
    0

    Question Your insights needed!

    Code:
    #include <stdio.h>
    
    int main()
    {
      int array[10]={1,2,2,3,4,4,5,6,7,7}
      
      ...
    }
    How do you replace duplicate elements? How do you prompt the user to replace that specific array address?

  2. #2
    Join Date
    Oct 2005
    Posts
    745
    Rep Power
    0

    Default

    What would you like to replace them with? Will the array always be sequential?
    3.14159265358979323846264338327950288
    4197169399375105820974944592307816406
    28620899862803482534211706798 pi 101

  3. #3
    Join Date
    Jul 2002
    Posts
    1,395
    Rep Power
    0

    Default

    So lets say the array was assembled from user input. How do you prune the array during the loading process, when the user is actually keying in the data?

    Where I am at right now:
    Code:
    /*Program contains an array of size 20. Allow user to load array.
    Ensure no duplication. Use at least 2 functions to achieve goal.
    Display content of array with location.*/
    #include <stdio.h>
    #define SIZE 20
    
    int main(void)
    {
    	int array[SIZE];
    	int c;
    	for (c=0; c<SIZE; c++)
    	{
    		printf("Enter an integer:\n");
    		/* something here to check if input is same as previous*/
    		scanf("%i",&array[c]);
    	}
    	printf("%s\t\t%s\n","Array subscript","Value");
    	for(c=0; c<SIZE; c++)
    	{
    		printf("%8d\t\t%2d\n",c,array[c]);
    	}
    }
    Last edited by carey; Mar 19, 2011 at 12:29 AM. Reason: merged multiple posts

  4. #4
    Join Date
    Oct 2005
    Posts
    745
    Rep Power
    0

    Default

    The %i in scanf should be %d.

    You'd really want to check after scanf as well since that's where you read the number.

    Instead of a for loop you could use a while loop and only proceed if the number read is not equal to array[i-1]. You might therefore want to use a temporary variable to hold the number and check it before assigning it to the array. Also, watch out for index 0 since there is no index at -1
    3.14159265358979323846264338327950288
    4197169399375105820974944592307816406
    28620899862803482534211706798 pi 101

  5. #5
    Join Date
    Jul 2002
    Posts
    1,395
    Rep Power
    0

    Default

    Quote Originally Posted by recursion View Post
    The %i in scanf should be %d
    These are identical according to documents.

    You'd really want to check after scanf as well since that's where you read the number.
    Doesn't scanf assign the value to the variable right away?

    Instead of a for loop you could use a while loop and only proceed if the number read is not equal to array[i-1].
    We are instructed that the 'for loop' is the same mechanism, but cleaner because it's a closed function(italicized words mine)
    You might therefore want to use a temporary variable to hold the number and check it before assigning it to the array. Also, watch out for index 0 since there is no index at -1
    So the idea is to store the first input into a variable, pass it to the array, then compare every other input afterwards?

  6. #6
    Join Date
    Oct 2005
    Posts
    745
    Rep Power
    0

    Default

    Quote Originally Posted by carey View Post
    These are identical according to documents.
    I was wondering about that, didn't check. Cool

    Quote Originally Posted by carey View Post
    Doesn't scanf assign the value to the variable right away?
    Yes it does. I was saying that the comment /* something here to check if input is same as previous*/ (or code that should replace it) should be after you've read the variable.


    Quote Originally Posted by carey View Post
    We are instructed that the 'for loop' is the same mechanism, but cleaner because it's a closed function(italicized words mine)
    They can both be written to behave the same way. What I was suggesting was the removal of the increment on every iteration, since if it is repeated you might not want to move down the array.

    Quote Originally Posted by carey View Post
    So the idea is to store the first input into a variable, pass it to the array, then compare every other input afterwards?
    Store the first in the array, then for the others, store them in a variable and compare with the previous index before inserting them.
    3.14159265358979323846264338327950288
    4197169399375105820974944592307816406
    28620899862803482534211706798 pi 101

  7. #7
    Join Date
    Jul 2002
    Posts
    1,395
    Rep Power
    0

    Default

    Quote Originally Posted by recursion View Post
    They can both be written to behave the same way. What I was suggesting was the removal of the increment on every iteration, since if it is repeated you might not want to move down the array.

    Store the first in the array, then for the others, store them in a variable and compare with the previous index before inserting them.
    Ok, I think I understand. Instead of using a for loop, use a process to set the first value, then use a function to compare and insert the rest. Going to get to it in a few. Will post what I come up with.

  8. #8
    Join Date
    Jul 2002
    Posts
    1,395
    Rep Power
    0

    Default

    Still stuck. I want to:
    1. watch for duplicates in the array
    2. prompt the user to replace the duplicate

  9. #9
    Join Date
    Mar 2006
    Posts
    325
    Rep Power
    0

    Default

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    #define SIZE 10;
    
    void function(int);
    int array[SIZE],c=0,x,val;
    
    int main()
    {
    	
    	
    	for (c=0; c<SIZE; c++)
    	{
    		printf("Enter an integer:\n");
                    scanf("%d",&val);
                    function(val); 
    	}
    	printf("%s\t\t%s\n","Array subscript","Value");
    	for(c=0; c<SIZE; c++)
    	{
    		printf("%8d\t\t%2d\n",c,array[c]);
    	}
     getch();
     return 0;
    }
    
    void function(int val)
    {
    	for(x=0;x<SIZE;x++) /* something here to check if input is same as previous*/
    	{
              if (array[x]==val)
              {
               printf("enter a new value");
               scanf("%i",&array[x]);
              } 
    	} 
    
    
    }
    Last edited by wiel; Mar 20, 2011 at 11:58 AM. Reason: globalize!!!
    1337

  10. #10
    Join Date
    Jul 2002
    Posts
    1,395
    Rep Power
    0

    Default

    That seems like it!
    I adapted it a bit.

    I think I better post my entire source code:
    Code:
    /*Program contains an array of size 20. Allow user to load array.
    Ensure no duplication. Use at least 2 functions to achieve goal.
    Display content of array with location.*/
    #include <stdio.h>
    #define SIZE 20 //this is the array size originally
    
    void table_head(void);// for output table
    int dup_check();// duplication checker
    int array[SIZE];
    
    int main(void)
    {
    
    	int c;// for incrementing
    	int check,newone;
    
    	//for (c=0; c<SIZE; c++)
    	c=0;
    	while (c<SIZE)
    	{
    		printf("Enter an integer:\n");
    		if (c==0)
    		{
    			scanf("%i",*array);//first entry not duplicate
    			continue;
    		}
    		scanf("%i",&check);
    		dup_check();
    		array[c]=newone;
    		c++;
    	}
    
    	table_head();
    	for(c=0; c<SIZE; c++)
    	{
    		printf("%8d\t\t%2d\n",c,array[c]);
    	}
    }
    
    void table_head(void)
    {
    	printf("%s\t\t%s\n","Array subscript","Value");
    }
    
    int dup_check(int check)//Thanks to wiel on Techjamaica.com forums
    {
    	int x;
    	int newone;
    	for(x=0; x<SIZE; x++) /* something here to check if input is same as previous*/
    	{
    		if (array[x]==check)
    		{
    			printf("You already entered %i.\nEnter another integer:\n",check);
    			scanf("%i",&newone);
    			return newone;
    		}
    		else
    			array[x]=check;
    	}
    	
    }
    So far it doesn't run.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •