Thread: Help with removing element from an array

  1. #1
    Registered User
    Join Date
    Apr 2014
    Location
    New York
    Posts
    52

    Help with removing element from an array

    Hello all, I'm having a problem with my program. I posted and received help with this before, but I've got a different problem now. The old code imported a number of lines of text into an array, next choose a random number of the lines, then randomly choose which lines to put into another array.
    Now instead of randomly choosing the number of text lines, I wish to use a predefined number, which I called MAX_NUM. I also want to keep a total count of rand_array_num, which I have called total_array_num. Now the part I'm having trouble with is that total_array_num must never be greater than MAX_NUM. Less than or equal to is fine.
    What seems to be happening is, so long as total_array_num is less or equal to MAX_NUM, the loop will happen, even if that makes total_array_num greater than MAX_NUM.
    I tried to fix the problem by including an if(total_array_num>MAX_NUM) to subtract rand_text_count by 1, in order to remove the last elements in both arrays. This partially works; the last record is erased, however after this total_array_num is still greater than MAX_NUM.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    #define ARRAY_SIZE 10
    #define INPUT_TEXT_LENGTH 120
    #define MAX_NUM 10
    
    int dieroll(int small, int large);
    
    int main()
    {
       FILE *myfile;
       char input_text[INPUT_TEXT_LENGTH]; // input entire line from text file
       char *item; // individual string
       int input_text_count=0; // # of lines from text file
       int rand_text_count=0; // # of text lines after choosing random # of text lines
       int initial_array_num[ARRAY_SIZE]; // input only int from text file
       int rand_array_num[ARRAY_SIZE]; // array for randomly chosen initial_array_num
       int total_array_num=0; // total count of rand_array_num
       char initial_array_string[ARRAY_SIZE][ARRAY_SIZE]; // input only string from text file
       char rand_array_string[ARRAY_SIZE][ARRAY_SIZE]; // array for randomly chosen initial_array_string
       int y,x; //used in 'for' loops
       //int first_roll=0;  randomly choose number of text lines (in this case 1d6)
       int second_roll=0; // randomly choose which text lines
       int numbers[ARRAY_SIZE];
    
       srand((unsigned)time(NULL));
    
    //open file, fill array, close file
       myfile=fopen("C:\\Users\\eric\\Desktop\\c_code\\number_exp.txt","r");
       if(!myfile)// check to see if an error occurs trying to open file
       {
          puts("number_exp.txt not found!");
          return (1);
       }
       while(fgets(input_text,120,myfile))
       {
          //printf("%s",input_text);
          item=strtok(input_text," "); // name of the text string must be first
          initial_array_num[input_text_count]=atoi(item);
    
          item=strtok(NULL," "); // NULL must be every one after the first
          strcpy(initial_array_string[input_text_count],item);
    
          input_text_count++;
       }
       fclose(myfile);
    
       printf("MAX_NUM is: %d\n",MAX_NUM);
       //first_roll=dieroll(0,6)+1;  choose number of text lines
    
       for(y=0;y<input_text_count;y++)// initialize the array
        numbers[y]=0;
    
    // choose which lines of text and copy one array into another
     //for(x=0;x<first_roll;x++)
    
     do
     {
         do
         {
             second_roll=dieroll(0,input_text_count);
         }
         while(numbers[second_roll]);
         numbers[second_roll]=1;
    
         printf("\ntotal_array_num is: %d\n",total_array_num);
         total_array_num+=(rand_array_num[rand_text_count]=initial_array_num[second_roll]);
         printf("   rand_array_num[rand_text_count] is: %d\n",rand_array_num[rand_text_count]);
         printf("total_array_num is: %d\n",total_array_num);
    
         strcpy(rand_array_string[rand_text_count],initial_array_string[second_roll]);
         printf("rand_array_string[rand_text_count] is: %s\n",rand_array_string[rand_text_count]);
    
         rand_text_count++; // increments rand_text_count by 1 for each loop
    
       if(total_array_num>MAX_NUM)
       {
           rand_text_count--;
           printf("after decrement, total_array_num is: %d\n",total_array_num);
       }
     }
     while(total_array_num<=MAX_NUM);
    
        for(x=0;x<rand_text_count;x++)
        {
            printf("\n   rand_array_num[rand_text_count] is: %d\n",rand_array_num[x]);
            printf("rand_array_string[rand_text_count] is: %s\n",rand_array_string[x]);
        }
        printf("\ntotal_array_num is: %d\n\n",total_array_num);
        return 0;
    }
    
    int dieroll(int small, int large)
    {
        large -= small;
        return(rand()%large)+small;
    }
    
    /* items in .txt file
    1 one
    2 two
    3 three
    4 four
    5 five
    6 six
    7 seven
    8 eight
    9 nine
    10 ten
    */

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by shaddock
    What seems to be happening is, so long as total_array_num is less or equal to MAX_NUM, the loop will happen, even if that makes total_array_num greater than MAX_NUM.
    It sounds like you want strict less than rather than less than or equal.

    By the way, you need to break up your main function into smaller functions that do one thing and do it well. Note that your do while loop indentation is off, which can be misleading.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2014
    Location
    New York
    Posts
    52
    Thank you, that did the trick. This is just the test program. In my main program I have it separated into three functions. And I shall do a better job of indenting. Though in my code the formatting is fine, somehow when I copy and paste it here the formatting seems to go wrong. Could it be the indenting in my code?

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Quote Originally Posted by shaddock View Post
    Thank you, that did the trick. This is just the test program. In my main program I have it separated into three functions. And I shall do a better job of indenting. Though in my code the formatting is fine, somehow when I copy and paste it here the formatting seems to go wrong. Could it be the indenting in my code?
    Your editor may be using tab characters instead of spaces. I would recommend changing the settings in your editor to replace tabs with 2, 3, 4, or 8 spaces. Then when you copy code to here or any other editor or forum, your formatting will be consistent, or when someone else is maintaining your code. I usually use 4 spaces per tab unless I am editing/maintaining code written by someone else.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Insert element after the last element in array in c
    By amaturequestion in forum C Programming
    Replies: 3
    Last Post: 04-09-2015, 08:29 AM
  2. removing an element from an array
    By synhyborex in forum C Programming
    Replies: 7
    Last Post: 03-10-2011, 05:33 AM
  3. Removing an array in an element?
    By bluej322 in forum C Programming
    Replies: 4
    Last Post: 02-19-2011, 11:36 PM
  4. Removing element from vector?
    By jw232 in forum C++ Programming
    Replies: 5
    Last Post: 02-04-2008, 02:54 AM
  5. Segfault when removing element from list
    By jmd15 in forum C++ Programming
    Replies: 4
    Last Post: 04-05-2007, 12:25 PM