Thread: array insertion with condition

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    4

    Question array insertion with condition

    hello there! can anyone help me with this could code.any suggestions and ideas will be very much appriciated.

    [tag]

    Code:
    int insB4Odd(int LA[],int n,int num){
    [/I]/*this function inserts item num b4 the index position
           of the first odd integer in the array list LA w/ n elements*/
    
    int j=0;
    
    while(j<n){
     if(LA[j]%3==0)
    
    /*this is the part where i'm stuck*/
    }
    return(++n);
    }
    
    int insAftrEven(int LA[],int n,int num){
    [I]/*this is somewhat the same with the above function,but instead of inserting b4 odd,item num is inserted after even number in the array list*/
    
    int j=0;
    while(j<n){
     if(LA[j]%2==0)
    
    [/I]/*I'm also stuck here.If I can just get the B4Odd, there's gonna
     be no problem here*/
    }
    return (++n);
    }
    
    int delNum(int LA[],int n,int num){
    [I]/*deletes the integer num from the array list LA if it exists,
    otherwise no deletion is made*/
    
    int i;
    
    for(i=0;i<n;i++){
     if(LA[i]==num)
    
    [/I]/*What would be the next part?*/}
    return (--n);
    }
    
    int delAll(int LA[],int num){
    /*deletes all items that are not unique*/
    
    Just give me an idea on how to go about this function.
    
    [/tag]



    Thank you in advance.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    It's very tough to understand what you want us to help with. Try asking specific questions, like how you would accomplish a certain task. No one's going to answer questions like what you should do next if you don't tell us what you want the program to do overall.

  3. #3
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    hmmm. This seems like one of those code completion homework problems............
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Quote Originally Posted by joh23
    hello there! can anyone help me with this could code.any suggestions and ideas will be very much appriciated.
    Code:
    int insB4Odd(int LA[],int n,int num){
    /* This function inserts item num b4 the index position
        of the first odd integer in the array list LA w/ n elements */
    
        int j=0;
    
        while(j<n){
            if(LA[j]%3==0)
    
            /* This is the part where i'm stuck*/
        }
    
        return(++n);
    }
    Your test for oddness would classify six and twelve as odd numbers! A number is odd if it is not even so a good test is number % 2 != 0. Once you have found the place where you want to insert the new element, you need to make room for it. You can do this by using the memmove function to move all the subsequent elements forward.
    Code:
    int insAftrEven(int LA[],int n,int num){
    /* This is somewhat the same with the above function,but instead
       of inserting b4 odd,item num is inserted after even number in the
       array list */
    
        int j=0;
    
        while(j<n){
             if(LA[j]%2==0)
    
            /*I'm also stuck here.If I can just get the B4Odd,
               there's gonna be no problem here */
        }
    
        return (++n);
    }
    Yes, again use memmove to make room for your new element. I suggest you create a common insertion function that can be used by both insB4Odd and insAftrEven.
    Code:
    int delNum(int LA[],int n,int num){
    /* deletes the integer num from the array list LA if it exists,
       otherwise no deletion is made*/
    
        int i;
    
        for(i=0;i<n;i++){
            if(LA[i]==num)
    
            /*What would be the next part?*/
        }
    
        return (--n);
    }
    You can delete an element by moving all the subsequent elements back one position. Again memmove is the function to use.
    Code:
    int delAll(int LA[],int num){
    /* deletes all items that are not unique */
    
    Just give me an idea on how to go about this function.
    There are two approaches for finding duplicates. The first is for every element in the array search every other element searching for matches. This would be done with a nested loop. The other, more efficient, approach is to first sort the array and then walk through it looking for adjacent elements that are equal. You can sort an array with qsort. The caller of this function may not want the order of the array changed. In this case, you could use the first approach (simpler) or create a temporary array to find the duplicates and only alter the original to delete the required elements. Once you've found an element to delete, again you can use memmove. You could create a common delete function to be used by both this function and delNum.

    Note that I have formatted your code. Properly formatted code is far easier to read and update.

    Some things you should think about are:
    • Is there room in the array to add an element. This is probably a concern to the caller rather than your functions.
    • What happens if there are no even/odd/duplicate numbers in the array. Will the code fail or return an incorrect value?

    Good luck, if you need anymore specific help, please post back.
    Last edited by anonytmouse; 02-09-2005 at 10:17 PM.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    4
    thank you very much anonytmouse.

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    4
    i'm not very familiar with the qsort and memmove function. But I tried to look up from my C book, it has this syntax:
    Code:
      void memmove(void *dest,const void *src,size_t n);
    for the qsort:
    Code:
     void qsort(void *base,size_telem,size_t width,int (*fcmp)(const void *,const void*));
    are these correct?
    i have a question: what's the difference between scanf() and gets() ?

  7. #7
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    >>are these correct?
    For the most part.

    >>what's the difference between scanf() and gets() ?
    If you use scanf with the right format modifier then there's no difference. But for the most part, scanf is far more flexible and it's possible to make it safe. There's no way to make gets safe.
    Kampai!

  8. #8
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Read down this post until you see my response on qsort. Should clear that up for you.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 02:02 PM
  2. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  3. Quick, Partiotion, and Insertion Sort
    By silicon in forum C++ Programming
    Replies: 0
    Last Post: 05-18-2005, 08:47 PM
  4. Insertion Sort Problem
    By silicon in forum C++ Programming
    Replies: 1
    Last Post: 05-08-2005, 12:30 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM