Question

In: Computer Science

#include <stdio.h> #include <cmath> #ifndef M_PI #define M_PI 3.14159265358979323846 #endif #pragma warning (disable : 4996) int...

#include <stdio.h>
#include <cmath>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#pragma warning (disable : 4996)


int main() {
  
const char* filename = "samples.coe";
const int N = 1024;
FILE* file = fopen(filename, "w");
if (file == NULL) {
perror("fopen");
}

fprintf(file, "; These are 1024 sample values in range -1 to 1,\n");
fprintf(file, "; Sine Wave 0\n");

fprintf(file, "memory_initialization_radix = 10;\n");
fprintf(file, "memory_initialization_vector\n");

double values[N];
double delta = M_PI / (N - 1);
for (int i = 0; i < N; i++) {
if (i == 0) {
values[i] = 0.0;
}
else {
values[i] = values[i - 1] + delta;
}
}
double sin_values[N];
for (int i = 0; i < N; i++) {
sin_values[i] = sin(values[i]);
if (i != N - 1)
fprintf(file, "%f;\n", sin_values[i]);
else
fprintf(file, "%f\n", sin_values[i]);

}
fclose(file);


file = fopen(filename, "r");

char line[256];
int line_no = 0;
double max = 0.0000;
double min = 1.0000;
double total = 0.0;
while (fgets(line, sizeof(line), file)) {
line_no++;
if (line_no >= 4) {
double val;
scanf_s(line, "%lf;", &val);
if (val > max) {
max = val;
}
if (val < min) {
min = val;
}
total += val;

}
}
printf("max = %lf min = %lf mean = %lf", max, min, total / N);


}

why it doesn't work in visual studio?

could you fix the errors?

Solutions

Expert Solution

#include <stdio.h>
#include <cmath>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#pragma warning (disable : 4996)
#include<cstdio>

int main() {
  
const char* filename = "samples.coe";
const int N = 1024;
FILE* file = fopen(filename, "w");
if (file == NULL) {
perror("fopen");
}

fprintf(file, "; These are 1024 sample values in range -1 to 1,\n");
fprintf(file, "; Sine Wave 0\n");

fprintf(file, "memory_initialization_radix = 10;\n");
fprintf(file, "memory_initialization_vector\n");

double values[N];
double delta = M_PI / (N - 1);
for (int i = 0; i < N; i++) {
if (i == 0) {
values[i] = 0.0;
}
else {
values[i] = values[i - 1] + delta;
}
}
double sin_values[N];
for (int i = 0; i < N; i++) {
sin_values[i] = sin(values[i]);
if (i != N - 1)
fprintf(file, "%f;\n", sin_values[i]);
else
fprintf(file, "%f\n", sin_values[i]);

}
fclose(file);


file = fopen(filename, "r");

char line[256];
int line_no = 0;
double max = 0.0000;
double min = 1.0000;
double total = 0.0;
while (fgets(line, sizeof(line), file)) {
line_no++;
if (line_no >= 4) {
double val;
std::sscanf(line, "%lf;", &val);
if (val > max) {
max = val;
}
if (val < min) {
min = val;
}
total += val;

}
}
printf("max = %lf min = %lf mean = %lf", max, min, total / N);


}

The compiler of your visual studio does not compile line57: scanf_s(line, "%lf;", &val); You have used <stdio.h> which is the header of scanf_s and still error is thrown.That is because your compiler is not aware of scanf_s.

So try using scanf or std::sscanf with header #include<cstdio> instead of scanf_s.

I have attached the program by using std::sscanf for your reference. Hope this code works on your compiler.


Related Solutions

#include <stdio.h> #define MAX 8 //Byte = 8 bits void func_and(int a[], int b[], int result[])...
#include <stdio.h> #define MAX 8 //Byte = 8 bits void func_and(int a[], int b[], int result[]) { for(int i=0; i < MAX; i = i + 1){ result[i] = a[i] & b[i]; } } void func_or(int a[], int b[], int result[]) { for(int i=0; i < MAX; i = i + 1){ result[i] = a[i] || b[i]; } } void func_not(int a[], int result[]) { for (int i = 0; i < MAX; i = i + 1) { result[i]...
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter...
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter between 8 to 60 inches: "); scanf("%d",&diameter); // if(diameter>60 || diameter<=8){ // printf("Error! invalid input"); // exit(0); // } // else{ // float radius = diameter/2; // float volume = (4/3)*PI*radius*radius*radius; // printf("%.2f",volume); // } //check through the while loop if it is valid or in valid while(diameter>60 || diameter<=8){ printf("Invalid input Enter again: "); scanf("%d",&diameter); }    //caluclate the volume of sphere float...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x = 3;     i = fun(x);     printf("%d\n", i);     return 0; } int fun(int i) {      int res = 0;      res = pow (i , 3.0);      return ( res); }
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc != 2) || (sscanf(argv[1],"%d",&count) != 1)) { fprintf(stderr,"Usage: %s <integer>\n", argv[0]); exit(1); } pid_t pid1, pid2; while (count > 0) { pid1 = fork(); if (pid1 > 0) { pid2 = fork(); count = count - 2; } else if (pid1 == 0) { count = count - 1; } } exit(0); } Question #1 [2 pts] If the command-line argument passed to this...
#include <stdio.h> #include <stdlib.h> int play_game(int *); // Returns 0 if player won, 1 if the...
#include <stdio.h> #include <stdlib.h> int play_game(int *); // Returns 0 if player won, 1 if the computer won, 2 if there is a tie, and -1 if the player decides to quit int menu(int *); // Displays choices to user // Receives score array int main() { srand(42); // Seeding Random with 42 int score[3]; // Array keeping Player, Computer, and Tie Scores score [0] = 0; // Player - initialized to Zero score [1] = 0; // Computer -...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;     char fname[20];     //int sum = 0;     int i, j, k, tmp =0;     int num = 0;     int mass = 0;     int count = 0;     int fuel = 0;     int total = 0;     int M[1000];     char ch;     char buffer[32];     printf(" Input the filename to be opened : ");     scanf("%s",fname);     myFile = fopen(fname, "r");     if(myFile == NULL)     {         printf("Can't open file\n");     } while(1)     {         ch =...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating();...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating(); int main() { // declaring variables int size = 5; int jerseyNo[size]; int rating[size]; int i = 0, jno, rate; char option; /* Getting the inputs entered by the user * and populate the values into arrays */ for (i = 0; i < size; i++) { printf("Enter player %d's jersey number:", i + 1); jerseyNo[i] = getValidJerseyNumber(); printf("Enter player %d's rating:\n", i +...
#include <stdio.h> int main () { int value= 10; int value2= 5; value = value %2;...
#include <stdio.h> int main () { int value= 10; int value2= 5; value = value %2; printf("he FInal =value=%d\n", value); value +=3; printf("he FInal =value=%d\n", value); value ++; printf("he FInal =value=%d\n", value); value= ++value2; printf("he FInal =value=%d\n", value); value= value2--; printf("he FInal =value=%d\n", value); } what is output explain each print statement? exlain why?
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid;...
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid; // Main process's PID=42 pid = fork(); // creates process with PID=36 if (pid == 0) { pid_t pid2 = fork(); // creates process with PID=99 sleep(10); if (pid2 > 0) { sleep(10); exit(0); } else { sleep(30); printf("** ONE **\n"); exit(0); } } else { pid_t pid3 = fork(); // creates process with PID=71 if (pid3 == 0) { sleep(30); exit(0); } pid_t...
My current code that is not working correctly #include<stdio.h> #include<math.h> int main() { //variable declaration int...
My current code that is not working correctly #include<stdio.h> #include<math.h> int main() { //variable declaration int a,b,c,D,n; double x1,x2; double realPart, imagPart; do { // this set of code blocks prompts a message to the user and then read the integer entered and stores it as an integer printf("Enter a value for a:\n"); scanf("%d",&a); printf("Enter a value for b:\n"); scanf("%d",&b); printf("Enter a value for c:\n"); scanf("%d",&c); printf("You entered the Equation \n"); printf("%dx^2%+dx%+d=0\n",a,b,c); D = b*b - 4*a*c;    if (D<0)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT