Adding data end of array

This is a discussion on Adding data end of array within the C Programming forums, part of the General Programming Boards category; I know how you define an array and how you put data into them but how do I add data ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    12

    Adding data end of array

    I know how you define an array and how you put data into them but how do I add data to the end of the array. What I have is this:

    Code:
    //declared in main
        int newsetArray[10];
    
    int compare(char *c1, char *c2, char *newset)
    {
        int char1max = strlen(c1);
        int char2max = strlen(c2);
        for(i=0; i<char1max; i++){
        for(j=0; j<char2max; j++){
        if(i==j){
        for(h=0; h<10; h++){
        if(h==j){
        }
        else{
        
        add j to array in here
        
        }
        }
        }
        }
        }
        
    }
    Basically if things check out I want to add a number to an array. Is there an easy way of doing it? So I want to start at 0 and continue adding to the array every time I come back to the else statement.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Do you mean a static int variable? Where the static variable is initialized only on the first time in the function, and subsequent times in the function, the value of the static variable is remembered from the last time?

    If you show an example of what you want, instead of more code, then I'll know what you're trying to do, better.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    12
    Basically what am doing is comparing two sets of numbers. I compare the two sets of numbers first and if 2 numbers match I go into another loop where I bring in the array that am talking about. First I run a loop with this array and the matched number to make sure it is not in the array, and if it does not match anything in the array I want to add the number into the array.

    Now I realize I have to initialize the array first or otherwise I will get some wacky values. But where am stuck is how do I start adding to this array at 0 and then keep adding to it every time the above occurs.

    Here is a good example of what am trying to do but this is in PHP:

    arr[]=22;

    This just goes to the first empty slot in the array. Can this be done in C?
    Last edited by bman900; 11-06-2010 at 12:05 AM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This just goes to the first empty slot in the array. Can this be done in C?
    Yes, of course. In pseudo pseudo-code.

    Code:
    if(num1==num2) {
      scan the int array[] for the number
      (your for loop should store the first empty array element it finds, "just in case")
      if(not found) {
          add it to the first empty array[] element you found previously
      }
    }
    As you can see, this is easily done.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    12
    Thanks but I just realized my whole logic on this was flawed. Back to the drawing board.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  3. Need help in C programming (too lazy)
    By cwillygs in forum C Programming
    Replies: 12
    Last Post: 04-20-2010, 12:23 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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