I need to take the numbers listed in the two files "numbers1.txt" and "numbers2.txt" (where the numbers are listed in ascending order) and move them to a file named "output.txt" where they are arranged in ascending order.
Here is what I have so far:
void appendToOutput(FILE *numFile1, FILE *numFile2, FILE *outputFile)
{
int num1 = 0;
int num2 = 0;
do {
fscanf(numFile1, "%d", &num1);
fscanf(numFile2, "%d", &num2);
while ((num1 < num2) && !feof(numFile1))
{
fprintf(outputFile, "%d\n", num1);
fscanf(numFile1, "%d", &num1);
}
while ((num2 < num1) && !feof(numFile2))
{
fprintf(outputFile, "%d\n", num2);
fscanf(numFile2, "%d", &num2);
}
if (num1 == num2)
{
fprintf(outputFile, "%d\n%d\n", num1, num2);
}
} while (!feof(numFile1) || !feof(numFile2));
return;
}
my files look like the following:
numbers1.txt
1
2
3
4
5
6
7
8
9
10
11
12
numbers2.txt:
2
4
6
8
10
12
14
16
18
20
22
24
The issue I am having is that the output file ends up looking like this:
output.txt
1
2
2
3
4
4
5
6
6
7
8
8
9
10
10
11
12
12
So my issue is that the program is not continuing to read/write numbers from numbers2.txt even though it has not yet hit the end of it's file. I've looked through it and I can't seem to find out why it's stopping, so help would be appreciated!
There are a few problems with the current code
Initialization of the 2 numbers should be performed before the main loop
Use of feof() here
Test of strict < insteaf of <= (could lock your algo)
Keep reading a file if the other one becomes empty
Regarding 2., fscanf returns a number >0 if it could read the number, so you can test this instead of checking feof (that says that EOF (a stop condition) happened before, and this might trigger one more unwanted iteration).
Suggested fixed code:
int read1 = fscanf(numFile1, "%d", &num1);
int read2 = fscanf(numFile2, "%d", &num2);
while (read1 > 0 || read2 > 0) {
while (read1 > 0 && (read2<=0 || num1 <= num2))
{
fprintf(outputFile, "%d\n", num1);
read1 = fscanf(numFile1, "%d", &num1);
}
while (read2 > 0 && (read1<=0 || num2 <= num1))
{
fprintf(outputFile, "%d\n", num2);
read2 = fscanf(numFile2, "%d", &num2);
}
}
Further explanations
readX<=0 || numY <= numX addresses 4.
readX = fscanf(...) addresses2.
Related
I'm having trouble with outputting a number pattern from recursion. I have correctly outputted some of the right numbers, but I can't get it to output the negative number and some of the missing numbers. This is the example input and output for my assignment:
Ex. If the input is:
12
3
the output is:
12 9 6 3 0 -3 0 3 6 9 12
This is my code:
int PrintNumPattern(int num1, int num2) {
printf("%d ", num1);
if (num1 <= 0) {
return 0;
}
else {
PrintNumPattern(num1 - num2, num2);
}
printf("%d ", num1);
}
int main(void) {
int num1;
int num2;
scanf("%d", &num1);
scanf("%d", &num2);
PrintNumPattern(num1, num2);
}
This is the output I keep getting from my code:
12 9 6 3 0 3 6 9 12
Your code just needs a few tweaks to provide the spirit of what you are after. Following is your code with some minor revisions.
#include <stdio.h>
#include <stdlib.h>
void PrintNumPattern(int num1, int num2) {
if (num1 < 0) {
num2 = num2 * -1; /* Reverse the sign to start incrementing back up */
}
else {
printf("%d ", num1); /* Put this here to skip a redundant repeated print */
PrintNumPattern(num1 - num2, num2);
}
printf("%d ", num1);
}
int main(void) {
int num1;
int num2;
scanf("%d", &num1);
scanf("%d", &num2);
PrintNumPattern(num1, num2);
printf("\n");
}
Here are the highlights.
When testing that the value has gone negative, there just needs to be a check for a negative number and not zero value. When that condition does occur, the second parameter needs to be changed to a negative number in order to effectively start adding instead of subtracting.
The initial "printf" statement is moved to be inside the "else" block so that it only prints out the negative number once.
In testing out these changes, the following is the output I saw on my terminal.
#Una:~/C_Programs/Console/Patterns/bin/Release$ ./Patterns
12
3
12 9 6 3 0 -3 0 3 6 9 12
#Una:~/C_Programs/Console/Patterns/bin/Release$ ./Patterns
22
4
22 18 14 10 6 2 -2 2 6 10 14 18 22
Experiment with that and see if that gives you what you want.
The posted code can't print -3 because it can't reach that value in the recursive calls
printf("%d ", num1);
if (num1 <= 0) { // As soon as num1 reaches 0, it stops the recursion
return 0;
}
else {
PrintNumPattern(num1 - num2, num2);
}
printf("%d ", num1); // This is skipped too.
To obtain the expected output, the recursion should be stop only when num1 becomes negative:
void PrintNumPattern(int num1, int num2)
{
printf("%d ", num1);
if (num1 >= 0)
{
PrintNumPattern(num1 - num2, num2);
printf("%d ", num1);
}
}
Testable here.
Note that I changed the return type to void. In the posted code the returned value is never used and not all the paths in the function have a return statement.
By rearranging #Bob's function, this is (imho) somewhat easier to think about. Full credit to #Bob...
When num1 >= 0, print it, and then go deeper... Eventually the bottom is reached, and the recursion unwinds through the 2nd printf()...
void PrintNumPattern(int num1, int num2) {
if (num1 >= 0) {
printf("%d ", num1);
PrintNumPattern(num1 - num2, num2);
// This is not an actual instruction location,
// but (conceptually) 'here' is where execution resumes
// as the recursion 'unwinds'...
// Obvious why num1 is printed both going down and coming up
}
printf("%d ", num1);
}
int main(void) {
PrintNumPattern( 12, 3 );
printf("\n");
return 0;
}
I have given a txt file that contains barcodes of product. I have to find which barcode is the second largest among the barcodes and have to tell how many times it has appeard in the file.
for example:
1 2 3 4 5 6 7 8 9 5 0 6
1 2 3 4 5 6 7 8 9 5 0 6
1 2 3 4 5 6 7 8 9 5 0 6
1 2 3 4 5 6 7 8 9 5 0 6
1 2 3 4 5 6 7 8 9 5 0 7
1 2 3 4 5 6 7 8 9 5 0 7
1 2 3 4 5 6 7 8 9 5 0 7
1 2 3 4 5 6 7 8 9 5 0 8
[I have to search from a big txt file that has more than 100 barcodes]
my output should be
Second largest barcode in terms of number : 1 2 3 4 5 6 7 8 9 5 0 7
It has appeard 3 times.
I have done so far :
#include <stdio.h>
#include <stdlib.h>
int main()
{
int one ,two, three, four, five, six, seven, eight, nine, ten, ele, twel;
int one2 ,two2, three2, four2, five2, six2, seven2, eight2, nine2, ten2, ele2, twel2;
char temp[256], name[30];
FILE *in;
if((in = fopen("bar.txt", "r")) == NULL)
{
printf("ERROR");
}
int count = 0;
int count2 = 0;
int finalcount = 0;
int max;
while (!feof(in))
{
fscanf(in, "%d %d %d %d %d %d %d %d %d %d %d %d",&one, &two, &three, &four, &five, &six, &seven, &eight, &nine, &ten, &ele, &twel);
count++;
//to print all the barcodes:
//printf("%d %d %d %d %d %d %d %d %d %d %d %d\n",one ,two, three, four, five, six, seven, eight, nine, ten, ele, twel);
while((!feof(in)))
{
max = finalcount; // trying to find the maximum count first.
fscanf(in, "%d %d %d %d %d %d %d %d %d %d %d %d",&one2, &two2, &three2, &four2, &five2, &six2, &seven2, &eight2, &nine2, &ten2, &ele2, &twel2);
if(one == one2 && two == two2 && three == three2 && four == four2 && five == five2 && six == six2 && seven == seven2 && eight == eight2 && nine == nine2 && ten == ten2 && ele == ele2 && twel == twel2);
{
if(finalcount > max)
{
max = finalcount;
}
finalcount++;
}
if (feof(in))
break;
}
if (feof(in))
break;
}
fclose(in);
//printf("Count2 %d",count2);
printf("Final count %d ", finalcount);
printf("Max count %d ", max);
return 0;
}
I wanted to take one line of the numbers and try to compare it with the rest. But it doesn't work properly.
I am very new at file handeling, I think I have problem in my logic as well a code structure.
is there a better way to write the code of the problem?
You can achieve your goal in these simple steps(assuming barcode is in number format):-
Open your file in read mode and check for any error while opening the file.
Read numbers from your file and store them in an array.
Sort the array in ascending or descending order.
According to your sorting technique either array[1] in descending order or array[n-2] in ascending order will be the second largest barcode.
Now, using a loop count the appearance of that barcode in the array.
Print your output.
Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
// There are 10 no. inside barcode.txt
void bubbleSort(long int *array, int n)
{
int i, j, temp;
for(i = 0; i < n; ++i)
{
for(j = i+1; j < n; ++j)
{
if(array[i] <= array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
// main()
int main()
{
FILE *barcode;
long int buffer[SIZE], second_largest = 0;
int i = 0, appearance = 0;
barcode = fopen("barcode.txt", "r");
if( !barcode )
{
perror("barcode.txt");
exit(-2);
}
// Storing data into buffer.
while(fscanf(barcode, "%ld", &buffer[i++]) != EOF);
// Sorting buffer in descending order.
bubbleSort(buffer, SIZE);
second_largest = buffer[1];
for ( i = 0; i < SIZE; ++i)
{
if(second_largest == buffer[i])
appearance++;
}
printf("Second largest barcode %ld appeared %d times in the file.\n", second_largest, appearance);
fclose(barcode);
return 0;
}
File contains:
21467
17283
27653
9019
26069
13826
19323
20289
13516
14657
Output:
Second largest barcode 26069 appeared 1 times in the file.
Is it 1 & 0 are prime numbers ? because when i Input 1 & 0 it says that it is a PRIME
#include <stdio.h>
int main(){
int num, i,y = 0;
printf("Enter a Number: ");
scanf("%d",&num);
for(i = 2; i <= num /2; ++i){
if(num % i == 0){
y=1;
}
}
printf("the number %d is a ",num);
if (y == 0){
printf("(PRIME)");
}
if(num % 2 == 0){
printf("(EVEN)");
}else
printf("(ODD)");
printf(" Number.");
}
can anybody help me with my code
No, neither 0 nor 1 are considered to be prime numbers, that is, the lowest prime number is 2. You need to change the code into, for instance:
if (y == 0 && n >= 2)
this covers both 0 and 1 along with negative integers (thanks to #MikeCAT)
I am working on a program to organize a list of numbers from a file and output these numbers in a easier to read format. Such as a file called Counting.txt containing the numbers:
11
1 1 2 3 4 4 4 4 5 5 7
and I want it to output:
1x2 2x1 3x1 4x4 5x2 7x1
The formula for output being vXc, where v is the number and c is the number of times it occurs. But my current program only outputs it as:
1x1 2x1 3x1 4x1 4x1 4x1 4x1 5x1 5x1 7x1
I believe there is a small error in my for loop that doesn't allow me to change my c variable, or the number indicating how many times the actual number occurs. Can anyone help?
My code:
#include <stdio.h>
int main () {
FILE* file = fopen("counting.txt", "r");
int total_num, count = 1, num, num2, i;
if (file == NULL) {
printf("Did not find counting.txt file.\n");
}
fscanf(file, "%d", &total_num);
fscanf(file, "%d", &num);
for (i = 1; i < total_num; i++) {
fscanf(file, "%d", &num);
if (num2 == num) {
count = count + 1;
} else {
printf("%dX%d ", num, count);
count = 1;
}
}
return 0;
}
In the loop you read into num instead of num2. So in general num2 is undefined. You also need at the end of each loop's iteration to assign to num the value of num2.
Also when you print the number of repetitions you should refer to the old value and not the current one, since you don't know if the current number will be followed by other equal numbers.
So you could change your loop to:
for (i=1; i< total_num; i++) {
fscanf(file, "%d", &num2);
if (num2 == num) {
count = count + 1;
}
else {
printf("%dX%d ", num, count);
count = 1;
}
num = num2;
}
printf("%dX%d ", num, count);
how do I detect if a user input int is between 2 numbers?
I've tried doing this:
int x=3;
printf("Enter the size of the triangle: ");
scanf("%d", &size);
odd=size%2;
for(x=3;x<21;x++)
{
if(x==size&&odd==1)
{
break;
}
else
{
printf("The size must be an odd number and be between\n3 and 21, inclusive, please try again\n\n");
printf("Enter the size of the triangle: ");
scanf("%d",&size);
odd=size%2;
x=3;
}
}
But the only input I can use is 3.
You already have all the bits of the solution in the code:
if (size >= 3 && size <= 21) {
// size is between 3 and 21 inclusive
} else {
// size is less than 3 or more than 21
}
If you also want to ensure it's odd, you can add the condition:
if ((size >= 3) && (size <= 21) && (size % 2 == 1)) {
// size is between 3 and 21 inclusive, and odd
} else {
// size is less than 3 or more than 21 or even
}
If your need is to continuously ask the user for a number until they enter one that's:
odd; and
between 3 and 21 inclusive,
you can use something like:
printf ("Enter the size of the triangle: ");
scanf ("%d", &size);
while ((x < 3) || (x > 21) || (x % 2 == 0)) {
printf ("The size must be an odd number and be between\n"
"3 and 21, inclusive, please try again.\n\n");
printf ("Enter the size of the triangle: ");
scanf ("%d", &size);
}
This is probably the simplest form. It gets the number then enters the while loop until it's valid.
You could refactor the printf/scanf pair into a separate function but that's probably not so important in a small snippet like this.
How about:
if(x >= 3 && x <= 21 && x%2) {
...
}
printf("Enter the size of the triangle");
scanf("%d", &size);
if (3 <= size && size <= 21 && size % 2) {
// size is between 3 and 21 (inclusive) and odd
}