P
Peter Dragun
I am generally new to programming under Unix, but know how to code under
Windows. I have to create a simple program, that takes the following
information from a file using redirection:
4 4 4 4 4 4 5 5 5 5 5 5 3 3 3 3 3 3 3 3 2 2 2 10 10 10 1 1
3 3 3 3 3 3 3 3 3 2 2 2 12 12 12 1 7 7 7 7 4
12 13 13 5 5 5 3 3 9 7 7 7 5 4 4 4 3 3 3 3
read each line, and "compress the information" to the following out print to
be printed on screen:
6 4
6 5
8 3
3 2
3 10
2 1
9 3
3 2
3 12
4 7
1 4
1 12
2 13
3 5
2 3
1 9
3 7
1 5
3 4
4 3
So the first column counts the occurences, and the second column in the
integer occurence.
I keep on getting a segmentation fault when I run my program, here is the
source code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* NUMBER, and COUNT are used in numbers array, indicating what is stored
*where
*/
#define NUMBER 0
#define COUNT 1
/* boolean definitions*/
#define TRUE 1
#define FALSE 0
/*flag to make sure first entry is processes properly*/
int first_run = TRUE;
/* next=store the next value;
* prev= stores the previous value from the stream;
* count=keep track how many times the same number is in sequence;
* incr_x = keeps track where to store the numbers in the numbers array;
* incr = used to display the numbers array, when encoding is complete.
*/
int next, prev, count, incr_x, incr;
/* Stream string to store input to be encoded*/
char stream[900] = "4 4 4 4 4 2 2 6 6 6 6 6 6 6 6 6 4 4 4 4 4 10 10 10 7 7 7
7 7 4 4";
/* Special delimiter to differient between values, this array is used with
* strtok function, the delimiter is set to whitespaces, so when a
whitespace
* is found, it indicates that the next element is a number.
*/
char delimiters[2] = " ";
/* Token pointer stores the outcome created by strtok*/
char *token;
/* The numbers array stores the numbers and count of them
* A number is stored using numbers[incr][NUMBERS]=n, and the count of that
* number found in the sequence i stored by numbers[incr][COUNT]=c
*/
int numbers[100][2];
int main() {
/* Standard input, sending data to the steam array for processing.*/
// This is where i get the fault, the program works, if I take out
the while, and just use the gets, but it only gets one line from the
// file.
while( gets( stream ) != NULL ) {
/* Get the first element that is delimited by whitespace. */
token = strtok( stream, delimiters );
do {
next = atoi( token );
/* If it is a first run, their is not previous value, so we
* assign prev with next.
*/
if( first_run ) {
prev = next;
first_run = FALSE;
}
/* If next is equal to the previous value, we increment the
count.*/
if( next == prev ) {
count++;
/* If the next value does not equal the previous,
that indicates a new
* number was found in the stream, so we insert the previous
value
* into the numbers array, and store how many times it was found
* sequentially, and then we reset the count, and increment
the numbers
* array for the next value.
*/
} else {
numbers[incr_x][NUMBER] = prev;
numbers[incr_x][COUNT] = count;
count = 1;
incr_x++;
}
prev = next;
/* We stop when the stream is finished.*/
} while( ( token = strtok( NULL,delimiters ) ) != NULL );
/* Since the strtok ends the do.while loop, before we can
process the
* last number found, we do it here.
*/
numbers[incr_x][NUMBER] = next;
numbers[incr_x][COUNT] = count;
incr_x ++;
}
/* Print out our results. */
for( incr=0; incr < incr_x; incr++ ) {
printf( "%d %d\n", numbers[incr][COUNT],
numbers[incr][NUMBER] );
}
return 0;
}
I wrote a comment where, I think the fault is occurring. Essentially, the
instructions told us we could only use scanf and printf for our I/O, but I
couldn't see how to use scanf to get what I want, so I decided to side line
one spec, and use gets(). With that in mind, I am limited to the use of
functions, and do not want to stray further from using gets or scanf.
Thanks
Windows. I have to create a simple program, that takes the following
information from a file using redirection:
4 4 4 4 4 4 5 5 5 5 5 5 3 3 3 3 3 3 3 3 2 2 2 10 10 10 1 1
3 3 3 3 3 3 3 3 3 2 2 2 12 12 12 1 7 7 7 7 4
12 13 13 5 5 5 3 3 9 7 7 7 5 4 4 4 3 3 3 3
read each line, and "compress the information" to the following out print to
be printed on screen:
6 4
6 5
8 3
3 2
3 10
2 1
9 3
3 2
3 12
4 7
1 4
1 12
2 13
3 5
2 3
1 9
3 7
1 5
3 4
4 3
So the first column counts the occurences, and the second column in the
integer occurence.
I keep on getting a segmentation fault when I run my program, here is the
source code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* NUMBER, and COUNT are used in numbers array, indicating what is stored
*where
*/
#define NUMBER 0
#define COUNT 1
/* boolean definitions*/
#define TRUE 1
#define FALSE 0
/*flag to make sure first entry is processes properly*/
int first_run = TRUE;
/* next=store the next value;
* prev= stores the previous value from the stream;
* count=keep track how many times the same number is in sequence;
* incr_x = keeps track where to store the numbers in the numbers array;
* incr = used to display the numbers array, when encoding is complete.
*/
int next, prev, count, incr_x, incr;
/* Stream string to store input to be encoded*/
char stream[900] = "4 4 4 4 4 2 2 6 6 6 6 6 6 6 6 6 4 4 4 4 4 10 10 10 7 7 7
7 7 4 4";
/* Special delimiter to differient between values, this array is used with
* strtok function, the delimiter is set to whitespaces, so when a
whitespace
* is found, it indicates that the next element is a number.
*/
char delimiters[2] = " ";
/* Token pointer stores the outcome created by strtok*/
char *token;
/* The numbers array stores the numbers and count of them
* A number is stored using numbers[incr][NUMBERS]=n, and the count of that
* number found in the sequence i stored by numbers[incr][COUNT]=c
*/
int numbers[100][2];
int main() {
/* Standard input, sending data to the steam array for processing.*/
// This is where i get the fault, the program works, if I take out
the while, and just use the gets, but it only gets one line from the
// file.
while( gets( stream ) != NULL ) {
/* Get the first element that is delimited by whitespace. */
token = strtok( stream, delimiters );
do {
next = atoi( token );
/* If it is a first run, their is not previous value, so we
* assign prev with next.
*/
if( first_run ) {
prev = next;
first_run = FALSE;
}
/* If next is equal to the previous value, we increment the
count.*/
if( next == prev ) {
count++;
/* If the next value does not equal the previous,
that indicates a new
* number was found in the stream, so we insert the previous
value
* into the numbers array, and store how many times it was found
* sequentially, and then we reset the count, and increment
the numbers
* array for the next value.
*/
} else {
numbers[incr_x][NUMBER] = prev;
numbers[incr_x][COUNT] = count;
count = 1;
incr_x++;
}
prev = next;
/* We stop when the stream is finished.*/
} while( ( token = strtok( NULL,delimiters ) ) != NULL );
/* Since the strtok ends the do.while loop, before we can
process the
* last number found, we do it here.
*/
numbers[incr_x][NUMBER] = next;
numbers[incr_x][COUNT] = count;
incr_x ++;
}
/* Print out our results. */
for( incr=0; incr < incr_x; incr++ ) {
printf( "%d %d\n", numbers[incr][COUNT],
numbers[incr][NUMBER] );
}
return 0;
}
I wrote a comment where, I think the fault is occurring. Essentially, the
instructions told us we could only use scanf and printf for our I/O, but I
couldn't see how to use scanf to get what I want, so I decided to side line
one spec, and use gets(). With that in mind, I am limited to the use of
functions, and do not want to stray further from using gets or scanf.
Thanks