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
*/