A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

write a C program using data files. the input data Numbers.txt contains several values for the base and power on alternating lines
Q:

write a C program using data files. the input data Numbers.txt contains several values for the base and power on alternating lines

0

write a C program using data files. the input data Numbers.txt contains several values for the base and power on alternating lines.

Each time the program reads two numbers, computes x^n and stores the result in the output file Result.txt

below are examples of the input and output files:

Numbers.txt:

-------------

1.2345

5

12.345

3

2.0

16

 

Results.txt:

-------------

2.86=5^1.23

1881.36=3^12.34

65536.00=16^2.00

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(int argc, char *argv[])
{
//read any text file from currect directory
char const* const fileName = "Numbers.txt";

FILE* file = fopen(fileName, "r"); 
FILE* fileToWrite = fopen(fileName, "w"); 

if(!file){
printf("\n Unable to open : %s ", fileName);
return -1;
}

char line[100];
char num1[100];
char num2[100];
int counter=0;
while (fgets(line, sizeof(line), file)) {

if(counter%2==0)
num1=line;
else { 
    //here we make the power operator then write in Results.txt
    num2=line;
    fprintf (fileToWrite, "%d^%d=%d\n",num1,num2,pow(atoi(num1),atoi(num2)));
}
counter++;
}
fclose(fileToWrite);
fclose(file);
  
    return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now