Thread: Array Comparison and Modification

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    32
    Quote Originally Posted by std10093 View Post
    Exactly

    Of course you do
    Here's my solution (it work's ) but it became quite complex and due to the shifting the garbage value also shows.
    Now will think on R.Stiltskin's idea.

    Code:
    #include <stdio.h>#include <stdlib.h>
    #define MAXSIZE 10
    int compact(int[]);
    int main()
    {
        int num=0, numarray[MAXSIZE]={0,0,0,0,0,0,0,0,0,0}, i=0;
        int top = -1;
        printf("Please enter some numbers. Enter -1 to stop.\n");
        while(num != -1)
        {
            scanf("%d", &num);
            if(top==(MAXSIZE-1) || num == -1)
            {
                printf("Done !\n");
                break;
            }
            else
            {
                top+=1;
                numarray[top] = num;
            }
        }
    
    
        while(i<MAXSIZE)
        {
            printf("%d ", numarray[i]);
            i++;
        }
        printf("\nPrinting compact numvers.\n");
        compact(numarray);
        getchar();
        return 0;
    }
    int compact(int changeArray[MAXSIZE])
    {
        int i=0, j=0, k=0, l=0;
        while(l<MAXSIZE)
        {
            i=0, j=0;
            while(i < MAXSIZE)
            {
                if(changeArray[i]==changeArray[i+1])
                {
                    j=i;
                    while(j < MAXSIZE)
                    {
                        changeArray[j]=changeArray[j+1];
                        j++;
                    }
                }
    
    
                    i++;
            }
            l++;
        }
    
    
         while(k<MAXSIZE)
         {
             printf("%d ", changeArray[k]);
             k++;
         }
         getchar();
         return 0;
    }
    I did not implement the size returning function as I am not very satisfied with the garbage values creeping in. Maybe I will modify to store 0/-1 instead of garbage and check conflict if user inputs the same.
    Last edited by Debojyoti Das; 12-21-2012 at 01:45 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Character Array Comparison
    By programit in forum C Programming
    Replies: 2
    Last Post: 01-31-2011, 12:30 PM
  2. Help on array comparison
    By sivapc in forum C++ Programming
    Replies: 6
    Last Post: 11-10-2009, 09:54 AM
  3. Scanf confusion, 2 dimensional array modification
    By Leojeen in forum C Programming
    Replies: 23
    Last Post: 10-19-2008, 10:58 PM
  4. array comparison
    By cloudy in forum C Programming
    Replies: 2
    Last Post: 10-16-2004, 02:45 PM
  5. array comparison
    By battoujutsu in forum C Programming
    Replies: 12
    Last Post: 12-05-2003, 11:47 AM

Tags for this Thread