Can anyone help me fix my C code so that it can accept answers in two decimals for division and integers in all other calculation?
Printable View
Can anyone help me fix my C code so that it can accept answers in two decimals for division and integers in all other calculation?
Code:/* mymaths.c
* This program plays a small math game allowing the player to work with +, -, / and *
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int c, v4;
double v1, v2, v3;
{
srand((unsigned)time (NULL));
do
{
printf("This program plays a small math game allowing the player to work with addition, subtraction, division and multiplication\n");
printf("\n");
printf("(For division, please answer in two decimals)\n");
printf("\n");
printf("Choose one of the following options:\n");
printf("1: Play with addition\n");
printf("2: Play with subtraction\n");
printf("3: Play with division\n");
printf("4: Play with multiplication\n");
printf("5: Quit program\n");
printf("Enter option: ");
scanf("%d", &c);
if (c==1)
{
v1 = 1.00 + rand() % 10;
v2 = 1.00 + rand() % 10;
v3 = v1 + v2;
printf("The answer for %2.2lf + %2.2lf is ", v1, v2);
scanf("%d", v4);
fflush(stdin);
if (v4 == v3)
{
printf("Well done, your correct\n");
printf("\n");
}
else{
printf("Sorry, your wrong. %2.2f + %2.2f = %.2f\n", v1, v2, v3);
printf("\n");
}
}
else if (c==2)
{
v1 = 1 + rand() % 10;
v2 = 1 + rand() % 10;
v3 = v1 - v2;
printf("The answer for %1.2lf - %1.2lf is ", v1, v2);
scanf("%d", &v4);
fflush(stdin);
if (v4 == v3){
printf("Well done, your correct\n");
printf("\n");
}
else{
printf("Sorry, your wrong. %2.2lf - %2.2lf = %1.2lf\n", v1, v2, v3);
printf("\n");
}
}
else if (c==3)
{
v1 = 1 + rand() % 10;
v2 = 1 + rand() % 10;
v3 = (v1 / v2);
printf("The answer for %2.2lf / %2.2lf is ", v1, v2);
scanf("%d", &v4);
fflush(stdin);
if (v4 == v3){
printf("Well done, your correct\n");
printf("\n");
}
else{
printf("Sorry, your wrong. %2.2lf / %2.2lf = %1.2lf\n", v1, v2, v3);
printf("\n");
}
}
else if (c==4)
{
v1 = 1 + rand() % 10;
v2 = 1 + rand() % 10;
v3 = v1 * v2;
printf("The answer for %2.2lf * %2.2lf is ", v1, v2);
scanf("%d", &v4);
fflush(stdin);
if (v4 == v3){
printf("Well done, your correct\n");
printf("\n");
}
else{
printf("Sorry, your wrong. %2.2lf * %2.2lf = %2.2lf\n", v1, v2, v3);
printf("\n");
}
}
else if (c==5)
{
printf("%d is not an option. Please select again\n", c);
return 0;
}
}while (c != 5);
}
}/* end main*/
fflush(stdin);
Do not flush input streams - read FAQ
so you know how to read int from user.
do read double - you will use %lf format of the scanf
in your comparison - better use only integers like
Code:if (c==1) /* addition */
{
int v1 = 1 + rand() % 10;
int v2 = 1 + rand() % 10;
int v3 = v1 + v2;
int v4;
printf("The answer for %d + %d is ", v1, v2);
fflush(stdout);
scanf("%d", &v4);
if (v4 == v3)
{
printf("Well done, your correct\n\n");
}
else
{
printf("Sorry, your wrong. %d + %d = %d\n\n", v1, v2, v3);
}
}
I already solved the problem for the other problems, but I have problems making the program accept two decimal places answer for division..... Any suggestion?
I don't understand exactly your final problem.
Additional suggestion;
If you need to use such a pattern as,
Instead of If-ElseIf, use Switch-Case structure for the sake of efficiency. There is serious differency between If-ElseIf and Switch-Case.Code:if (c==1)
{ C-Statements }
else if (c==2)
{ C-Statements }
else if (c==3)
{ C-Statements }
else if (c==4)
{ C-Statements }
else if (c==5)
{ C-Statements }
.
.
.
else if (c==n)
{ C-Statements }
else
{ C-Statements }
What I mean is that I want the program to be able to accept answers for the division problem in two decimal places and compare it with the correct answer... Any suggestions?
accept them as float, multiply by 100 and round to int - compare the resulting integers
This code have some problems in listing down the data entered by the user.. Can Aybody help?
Code:/*----------------------------------------------------------------------------------
*
* This program request the user to enter the number of data that will be entered,
* then outputs the mean(average) of the data entered and the largest and smallest
* data entered.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
int main(void)
{
char o;
int a[30], n, i, r;
double Total, mean, min, max;
max = -90000.00;
min = 90000.00;
Total = 0;
fflush(stdin);
do
{
printf("Welcome to Statistic Program\n");
printf("1 Run Statistic Program\n");
printf("2 Quit program\n");
printf("Please enter your option: ");
scanf("%d", &o );
if (o==1)
{
printf("How many numbers? ");
scanf("%d", &n);
if((n<2) || (n>30))
{
printf("Too many numbers or invalid numbers entered\n");
printf("\n");
return main();
}
printf("Enter the data one by one\n");
printf("n Data\n");
for(i=0; i< n; i++)
{
for(r = 0; r < n; r++)
{
printf("%d \t", r);
scanf("\t %d", &a[i]);
}
}
for(i=0; i<n; i++)
{
if(a[i]> max)
{
max = a[i];
}
}
for(i=0; i<n; i++)
{
if(a[i]< min)
{
min = a[i];
}
}
for(i=0; i<n; i++)
{
Total= Total + a[i];
mean = Total/n;
}
printf("\nMean of the values is %6.2f\n", mean);
printf("Maximum value is %6.2f\n", max);
printf("Minimum value is %6.2f\n", min);
printf("\n");
}
else if (o==2)
{
/*end Program*/
printf("Invalid option selected\n");
return 0;
}
}while (o != 2);
}/* end main*/
fflush(stdin); - it is undefined - read FAQ http://faq.cprogramming.com/cgi-bin/...&id=1043284351
scanf("%d", &o );
o should be int, not char for %d format - fix it to avoid problems
return main(); - never call main
you scan same number n times. Why? You think computer cannot understand you first time you enter that number?Code:for(r = 0; r < n; r++)
{
printf("%d \t", r);
scanf("\t %d", &a[i]);
}
mean = Total/n; - this could be done outside the loop
I want the program to list down the data entered as it is entered by the user.... Any suggestions?
Perhaps store the input in an array and then print the content of the array when you want it listed?
--
Mats
Can anyone give an example on how to make a code for this:
where the three data files have 5 point each for x-axis and y-axis.Quote:
two measurable quantities x and y will vary as
y=2x+3
Write a program that estimates whether a given set of five (x, y) pairs (representing data observed by the self-monitoring machine) read from a data file fits the “ok” line y=2x+3 closely enough. Generate 21 points evenly spread on the line, using x between 0 and 20. Then combine the field data read from the file with these generated points and compute the correlation coefficient for the entire data set. If the correlation coefficient falls below 0.98, issue a “possible chip failure” message. Otherwise, issue an “ok” message.
Note: three sample data files are provided for testing purpose.
Please help
Give us the exsample of what you have, we will help to fix errorsQuote:
Can anyone give an example on how to make a code for this:
Here is my code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int readData(double dataX[], double dataY[]);
int main(void)
{
double dataX[50], dataY[50];
char FILENAME;
printf("This program performs the self monitoring of a complex machine.\n");
printf("\n");
printf("\t Enter diagnosis data file name: ");
printf("\t ");
printf("\t Enter the slope of the OK line: ");
printf("\t Enter the y-intercept of the OK line: ");
printf("\t Enter lower bound of x value: ");
printf("\t Enter upper bound of x value: ");
printf("\t Enter the number of points to be generated: ");
return 0;
}
int readData(double dataX[], double dataY[])
{
FILE *dataFile;
int num_read = 0; /* The number of entries read from file */
/* Open input file. */
dataFile = fopen(FILENAME,"r");
/* Check if successful */
if (dataFile == NULL)
{
printf("Cannot open marks file for reading\n");
exit(1);
}
/* Read data . */
while (fscanf(dataFile, "%lf", &marks[num_read]) == 1)
num_read++;
return num_read;
}
Is there any reason why this code won't open and print the data in the file?
The file contents:Code:#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
char *rdata;
int j, k, n = 0; /* The number of entries read from file */
double arrayx[6], arrayy[6];
FILE *data;
printf("Name of file: ");
scanf("%c", &rdata);
data = fopen(rdata,"r");
/* Check if successful */
if (data == NULL)
{
printf("Cannot open %c file for reading\n", &rdata);
exit(1);
}
/* Read data . */
while (fscanf(data, "%lf", &n) == 1)
n++;
printf("n X Y\n");
for (j=0; j<n; j++)
{
printf("%d \t",j);
printf("\t %lf \t %lf", &arrayx[j], &arrayy[j]);
}
return 0;
}
Quote:
1.5 6.2
1.7 6.5
9.3 21.6
5.9 14.7
16.1 35.0
scanf("%c", &rdata);
rdata should be of type char, not char*
and it will not store the name - just one char
to store the name you need char array
char filename[MAX_PATH]
and use some suitable format, for example %s if name does not contains spaces
same goes for printf
Any reason why the data in my file cannot be displayed using this code? And is there a way to count how elements are there in a file?
The file contains:Code:#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define FILENAME "rdata.txt"
int main()
{
int readdata(double arrayx1[], double arrayy1[]);
int j, k, n; /* The number of entries read from file */
double arrayx[6], arrayy[6];
char *rdata;
FILE *data;
printf("File name: ");
scanf("%s", &rdata);
printf("n is 5\n", n);
data = fopen(FILENAME,"r");
for (j=0; j<5; j++)
{
fscanf(data, "%lf %lf", &arrayx[j], &arrayy[j]);
}
printf("n X Y\n");
for (j=0; j<5; j++)
{
printf("%d ",j);
printf("%.2lf %.2lf\n", arrayx[j], arrayy[j]);
}
return 0;
}
int readData(double arrayx1[], double arrayy1[])
{
FILE *data;
int n1 = 0; /* The number of entries read from file */
/* Open input file. */
data = fopen(FILENAME,"r");
/* Check if successful */
if (data == NULL)
{
printf("Cannot open marks file for reading\n");
exit(1);
}
/* Read data . */
while (fscanf(data, "%d"))
++n1;
return n1;
}
Quote:
1.5 6.2
1.7 6.5
9.3 21.6
5.9 14.7
16.1 35.0
And when you do fscanf(data, "%d"), where do you expect the read-in number to go exactly? And also: how do you expect to read in a number like 1.5 using %d?
cannot be displayed - is not very detailed decription of the problem
Your code does not have any error checking - check the return values of fopen and scanf
also scanf("%s", &rdata); is not healthy at all as was pointed several times already in the last days
The problem is when I define FILENAME to a certain file name(e.g diag000.txt) the code can print the data in the file nicely, but what I want is to let the user be able to enter the file name of the file they want too use... Any suggestions?
And also, I want the program to be able to calculate the number of data in the file for the first column only...
Please help...
Code:char buf[MAX_PATH];
if(fgets(buf, sizeof buf, stdin))
{
/* remove \n and open file */
}
What do you mean by "calculate the number of data"?Quote:
And also, I want the program to be able to calculate the number of data in the file for the first column only...
So, as vart pointed out, don't screw up the reading in of the filename as badly as you did:
(1) you need to have some memory available for what the user types in, such as char rdata[25]
(2) you need to pass in a pointer to the memory, not a pointer to a pointer to the memory (rdata vs. &rdata)
(3) there is no possible way to make %s be right; either use a number in the format specifier, such as %24s, or use fgets
As for the other, you need to know either (a) how many numbers are in each line, or (b) whether the number you just read in was immediately preceded by a new-line.
or just use pair fgets/sscanf so you know exactly how many lines are sucessfully read from file, and parsed into numbersQuote:
As for the other, you need to know either (a) how many numbers are in each line, or (b) whether the number you just read in was immediately preceded by a new-line.
I don't understand what do you mean by using fgets.... I just want the program to determine how many rows of data are available in a file then use that number to calculate the mean of the data....
And do you know what does "addMarx was not declared in this scope" as I am trying to plot the data in the file on a graph......
Please include examples on what I should do to solve these problems....
The data in my file is as follows:
Quote:
1.5 6.2
1.7 6.5
9.3 21.6
5.9 14.7
16.1 35.0
fgets is the "read from a file" command, which reads a line from a file.
"addMarx was not declared in this scope" means that addMarx was not declared in this scope, that is, you are using a variable you haven't declared.
Can anyone give me a working code that can determine the number of elements in pairs in a file?
an e.g of the file :
Quote:
1.5 6.2
1.7 6.5
9.3 21.6
5.9 14.7
16.1 35.0
This isn't going to work. Your file name isn't one character (%c).Quote:
printf("Name of file: ");
scanf("%c", &rdata);
Edit : Man I type too slow, and don't read the full thread before posting. :D
If you need to know number of lines in a file - use fgets to read a lineQuote:
Can anyone give me a working code that can determine the number of elements in pairs in a file?
and count number of successful calls
If you need to know the number of numbers in each line - use sscanf and check the return value
Next time asking question - show some code demonstrating your afforts
Can anyone fix my code?
My code stops working just before it starts calculating the variable a...
Here is my code:
Code:/*assign3_4201302.c
* Student Name : Syed Rizal Alfam
* Student ID : 4201302
*This program will read a set of data from a file, calulate the correlation with the ok-line and
*draw the graph of the ok-line and plot the set of data on the graph
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <koolplot.h>
int main()
{
/* Define variables */
int j, k, sl, yi, p, xy=5, n=p+xy,xp, xl, xu, sn; /* The number of entries read from file */
int readData(double arrayx[], double arrayy[], int j);
double a, b, cor, arrayx[50], arrayy[50], ssumy2, ssumny2, ssumnxy2, sumxy, sumx2;
double ssumx1, sMx, ssumy1, sMy, ssumx2, sa, ssumxy, ssumnxy, ssumnx2, sb, scor;
double getsumxy(double ssumx1, double sMx, double ssumy1, double sMy, int sn);
double getsumx2(double arrayx[], double sMx, double sMy, int sn);
double getb(double sMy, double sMx, double sa);
double getcor(double ssumxy, double ssumnxy, double ssumy2, double ssumx2, double ssumnx2,
double ssumny2, double ssumnxy2, int sn);
void printResults(double sa, double sb, double scor);
void graph(double arrayx[], double arrayy[]);
/* Read data from file */
xy = readData(arrayx, arrayy, j);
/*Enter details of Ok line*/
printf("Enter the slope of ok line: ");
scanf("%d", &sl);
printf("Enter the y-intercept of the ok line: ");
scanf("%d", &yi);
printf("Enter lower bound of x value: ");
scanf("%d", &xl);
printf("Enter upper bound of x value: ");
scanf("%d", &xu);
printf("Enter the number of points to be generated: ");
scanf("%d", &p);
printf("Points from ok line %dx + %d:\n", sl, yi);
printf("X Y\n");
for (j=xy; j<p+xy; j++)
{
xp=j-xy;
arrayy[j] = sl*xp + yi;
printf("%d %.2lf\n", xp, arrayy[j]);
}
/*Obtain value of a*/
sumxy = getsumxy(ssumx1, sMx, ssumy1, sMy, sn);
sumx2 = getsumx2(arrayx, sMx, sMy, sn);
a = sumxy / sumx2;
/*End of a*/
/*Obtain value of b*/
b = getb(sMy, sMx, sa);
/*Obtain value of cor*/
cor = getcor(ssumxy, ssumnxy, ssumy2, ssumx2, ssumnx2, ssumny2, ssumnxy2, sn);
/*Print results*/
printResults(sa, sb, scor);
/*Draw graph*/
graph(arrayx, arrayy);
/*Return*/
return 0;
}
/* Function readData
* Opens file and read data into marks array.
*
* Parameter: arrayx and arrayy - array of doubles to store data from file
*
* Return: number of entries read from file
*/
int readData(double arrayx[], double arrayy[], int j)
{
char rdata[25];
FILE *data;
printf("File name(must be typed with file type): ");
scanf("%s", &rdata);
j=0; /* The number of entries read from file */
/* Open input file. */
data = fopen(rdata,"r");
/* Check if successful */
if (data == NULL)
{
printf("Cannot open marks file for reading\n");
exit(1);
}
/* Read data . */
while (fscanf(data, "%.2lf %.2lf", &arrayx[j], &arrayy[j]) == 2)
j++;
return j;
}
double getsumxy(double sumx1, double Mx, double sumy1, double My, int n)
{
double a, arrayx[50], arrayy[50], sumxy;
int j;
/*Determine mean of x*/
sumx1 = 0.00;
for(j = 0; j < n; j++)
{
sumx1 = sumx1 + arrayx[j];
}
Mx = sumx1 / n;
/*End mean of x*/
/*Determine mean of y*/
sumy1 = 0.00;
for(j = 0; j < n; j++)
{
sumy1 = sumy1 + arrayy[j];
}
My = sumy1/n;
/*End mean of y*/
/*Determine sumxy*/
sumxy = 0.00;
for(j = 0; j < n; j++)
{
sumxy = sumxy + (arrayx[j] - Mx)*(arrayy[j] - My);
}
/*End of sumxy*/
return sumxy;
}
double getsumx2(double arrayx[], double Mx, double My, int n)
{
/*Determine sumx2*/
double sumx2 = 0.00;
int j;
for(j = 0; j < n; j++)
{
sumx2 = (sumx2 + ((arrayx[j] - Mx)*(arrayx[j] - Mx)));
}
/*End of sumx2*/
return sumx2;
}
double getb(double My, double Mx, double a)
{
double b;
/*Determine b:*/
b = 0.00;
b = (b + My) - (a * Mx);
/*End of b*/
return b;
}
double getcor(double sumxy, double sumnxy, double sumy2, double sumx2, double sumnx2,
double sumny2, double sumnxy2, int n)
{
double sumx1, Mx, sumy1, My, cor, arrayy[50];
double c, d;
int xy, p, j;
n = xy+p;
/*Determine sumnxy*/
sumnxy = 0.00;
sumnxy = sumnxy + (sumxy/n);
/*End of sumnxy*/
/*Determine sumy2*/
sumy2 = 0.00;
for(j = 0; j < n; j++)
{
sumy2 = sumy2 + ((arrayy[j] - My)*(arrayy[j] - My));
}
/*End of sumy2*/
/*Determine sumnx2*/
sumnx2 = 0.00;
c = 0.00;
c = c + (sumx2/n);
sumnx2 = sumnx2 + sqrt(c);
/*End of sumnx2*/
/*Determine sumny2*/
sumny2 = 0.00;
d = 0.00;
d = d + (sumy2/n);
sumny2 = sumny2 + sqrt(d);
/*End of sumny2*/
/*Determine sumnxy2*/
sumnxy2 = 0.00;
sumnxy2 = sumnxy2 + (sumnx2 * sumny2);
/*End of sumnxy2*/
/*Determine cor*/
cor = 0.00;
cor = cor + (sumnxy / sumnxy2);
/*End of cor*/
return cor;
}
/* Function printResults
* Display the value of a, b, and cor
* Determine the status of chip
*
* Parameter: geta - the value of a
* Parameter: getb - the value of b
* Parameter: cor - the value of correlation
* return - void
*/
void printResults(double a, double b, double cor)
{
printf("Value of a is %.2lf\n", a);
printf("Value of b is %.2lf\n", b);
printf("Value of correlation is %.2lf\n", cor);
if (cor < 0.98)
{
printf("Possible chip failure\n");
}
else
{
printf("Chip is OK\n");
}
}
/*Function graph
*Draws graph of ok line and
*plots the data from the file on the graph*/
void graph(double arrayx[], double array[])
{
plotdata x(xl, xu);
plotdata y = sl*x + yi;
for (j=0; j<xy; j++)
{
addMark(x, y, arrayx[j], arrayy[j]);
}
plot(x, y, BLACK, "SELF MONITORING: ok line: y = 2x + 3 Correlation:1 ->ok");
}
Try to fix warnings before continue your work - especially - about usage of not-initialized variables
Code:-*- mode: compilation; default-directory: "~/test3/" -*-
Compilation started at Tue Apr 29 01:40:35
make -k
icc -o test3.o -c -g -Wall test3.c
test3.c(11): warning #991: //-style comments are nonstandard
//#include <koolplot.h>
^
test3.c(17): warning #592: variable "p" is used before its value is set
int j, k, sl, yi, p, xy=5, n=p+xy,xp, xl, xu, sn; /* The number of entries read from file */
^
test3.c(18): warning #1419: external declaration in primary source file
int readData(double arrayx[], double arrayy[], int j);
^
test3.c(21): warning #1419: external declaration in primary source file
double getsumxy(double ssumx1, double sMx, double ssumy1, double sMy, int sn);
^
test3.c(22): warning #1419: external declaration in primary source file
double getsumx2(double arrayx[], double sMx, double sMy, int sn);
^
test3.c(23): warning #1419: external declaration in primary source file
double getb(double sMy, double sMx, double sa);
^
test3.c(24): warning #1419: external declaration in primary source file
double getcor(double ssumxy, double ssumnxy, double ssumy2, double ssumx2, double ssumnx2,
^
test3.c(26): warning #1419: external declaration in primary source file
void printResults(double sa, double sb, double scor);
^
test3.c(27): warning #1419: external declaration in primary source file
void graph(double arrayx[], double arrayy[]);
^
test3.c(31): warning #592: variable "j" is used before its value is set
xy = readData(arrayx, arrayy, j);
^
test3.c(54): warning #592: variable "ssumx1" is used before its value is set
sumxy = getsumxy(ssumx1, sMx, ssumy1, sMy, sn);
^
test3.c(54): warning #592: variable "sMx" is used before its value is set
sumxy = getsumxy(ssumx1, sMx, ssumy1, sMy, sn);
^
test3.c(54): warning #592: variable "ssumy1" is used before its value is set
sumxy = getsumxy(ssumx1, sMx, ssumy1, sMy, sn);
^
test3.c(54): warning #592: variable "sMy" is used before its value is set
sumxy = getsumxy(ssumx1, sMx, ssumy1, sMy, sn);
^
test3.c(54): warning #592: variable "sn" is used before its value is set
sumxy = getsumxy(ssumx1, sMx, ssumy1, sMy, sn);
^
test3.c(60): warning #592: variable "sa" is used before its value is set
b = getb(sMy, sMx, sa);
^
test3.c(63): warning #592: variable "ssumxy" is used before its value is set
cor = getcor(ssumxy, ssumnxy, ssumy2, ssumx2, ssumnx2, ssumny2, ssumnxy2, sn);
^
test3.c(63): warning #592: variable "ssumnxy" is used before its value is set
cor = getcor(ssumxy, ssumnxy, ssumy2, ssumx2, ssumnx2, ssumny2, ssumnxy2, sn);
^
test3.c(63): warning #592: variable "ssumy2" is used before its value is set
cor = getcor(ssumxy, ssumnxy, ssumy2, ssumx2, ssumnx2, ssumny2, ssumnxy2, sn);
^
test3.c(63): warning #592: variable "ssumx2" is used before its value is set
cor = getcor(ssumxy, ssumnxy, ssumy2, ssumx2, ssumnx2, ssumny2, ssumnxy2, sn);
^
test3.c(63): warning #592: variable "ssumnx2" is used before its value is set
cor = getcor(ssumxy, ssumnxy, ssumy2, ssumx2, ssumnx2, ssumny2, ssumnxy2, sn);
^
test3.c(63): warning #592: variable "ssumny2" is used before its value is set
cor = getcor(ssumxy, ssumnxy, ssumy2, ssumx2, ssumnx2, ssumny2, ssumnxy2, sn);
^
test3.c(63): warning #592: variable "ssumnxy2" is used before its value is set
cor = getcor(ssumxy, ssumnxy, ssumy2, ssumx2, ssumnx2, ssumny2, ssumnxy2, sn);
^
test3.c(66): warning #592: variable "sb" is used before its value is set
printResults(sa, sb, scor);
^
test3.c(66): warning #592: variable "scor" is used before its value is set
printResults(sa, sb, scor);
^
test3.c(17): warning #177: variable "k" was declared but never referenced
int j, k, sl, yi, p, xy=5, n=p+xy,xp, xl, xu, sn; /* The number of entries read from file */
^
test3.c(17): warning #177: variable "n" was declared but never referenced
int j, k, sl, yi, p, xy=5, n=p+xy,xp, xl, xu, sn; /* The number of entries read from file */
^
test3.c(19): warning #593: variable "a" was set but never used
double a, b, cor, arrayx[50], arrayy[50], ssumy2, ssumny2, ssumnxy2, sumxy, sumx2;
^
test3.c(19): warning #593: variable "b" was set but never used
double a, b, cor, arrayx[50], arrayy[50], ssumy2, ssumny2, ssumnxy2, sumxy, sumx2;
^
test3.c(19): warning #593: variable "cor" was set but never used
double a, b, cor, arrayx[50], arrayy[50], ssumy2, ssumny2, ssumnxy2, sumxy, sumx2;
^
test3.c(82): warning #1418: external function definition with no prior declaration
int readData(double arrayx[], double arrayy[], int j)
^
test3.c(87): warning #181: argument is incompatible with corresponding format string conversion
scanf("%s", &rdata);
^
test3.c(101): warning #269: invalid format string conversion
while (fscanf(data, "%.2lf %.2lf", &arrayx[j], &arrayy[j]) == 2)
^
test3.c(108): warning #1418: external function definition with no prior declaration
double getsumxy(double sumx1, double Mx, double sumy1, double My, int n)
^
test3.c(110): warning #177: variable "a" was declared but never referenced
double a, arrayx[50], arrayy[50], sumxy;
^
test3.c(142): warning #1418: external function definition with no prior declaration
double getsumx2(double arrayx[], double Mx, double My, int n)
^
test3.c(142): warning #869: parameter "My" was never referenced
double getsumx2(double arrayx[], double Mx, double My, int n)
^
test3.c(156): warning #1418: external function definition with no prior declaration
double getb(double My, double Mx, double a)
^
test3.c(167): warning #1418: external function definition with no prior declaration
double getcor(double sumxy, double sumnxy, double sumy2, double sumx2, double sumnx2,
^
test3.c(173): warning #592: variable "xy" is used before its value is set
n = xy+p;
^
test3.c(173): warning #592: variable "p" is used before its value is set
n = xy+p;
^
test3.c(170): warning #177: variable "sumx1" was declared but never referenced
double sumx1, Mx, sumy1, My, cor, arrayy[50];
^
test3.c(170): warning #177: variable "Mx" was declared but never referenced
double sumx1, Mx, sumy1, My, cor, arrayy[50];
^
test3.c(170): warning #177: variable "sumy1" was declared but never referenced
double sumx1, Mx, sumy1, My, cor, arrayy[50];
^
test3.c(224): warning #1418: external function definition with no prior declaration
scanf("%d", &xu);
^
test3.c(243): warning #1418: external function definition with no prior declaration
b = getb(sMy, sMx, sa);
^
test3.c(245): error: identifier "plotdata" is undefined
plotdata x(xl, xu);
^
test3.c(245): warning #92: identifier-list parameters may only be used in a function definition
plotdata x(xl, xu);
^
test3.c(245): warning #310: old-style parameter list (anachronism)
plotdata x(xl, xu);
^
test3.c(245): warning #1419: external declaration in primary source file
plotdata x(xl, xu);
^
test3.c(246): error: identifier "plotdata" is undefined
plotdata y = sl*x + yi;
^
test3.c(246): error: identifier "sl" is undefined
plotdata y = sl*x + yi;
^
test3.c(246): error: expression must have arithmetic type
plotdata y = sl*x + yi;
^
test3.c(246): error: identifier "yi" is undefined
plotdata y = sl*x + yi;
^
test3.c(247): error: identifier "j" is undefined
for (j=0; j<xy; j++)
^
test3.c(247): error: identifier "xy" is undefined
for (j=0; j<xy; j++)
^
test3.c(249): warning #266: function "addMark" declared implicitly
addMark(x, y, arrayx[j], arrayy[j]);
^
test3.c(249): error: identifier "arrayy" is undefined
addMark(x, y, arrayx[j], arrayy[j]);
^
test3.c(249): remark #981: operands are evaluated in unspecified order
addMark(x, y, arrayx[j], arrayy[j]);
^
test3.c(251): warning #266: function "plot" declared implicitly
plot(x, y, BLACK, "SELF MONITORING: ok line: y = 2x + 3 Correlation:1 ->ok");
^
test3.c(251): error: identifier "BLACK" is undefined
plot(x, y, BLACK, "SELF MONITORING: ok line: y = 2x + 3 Correlation:1 ->ok");
^
test3.c(251): remark #981: operands are evaluated in unspecified order
plot(x, y, BLACK, "SELF MONITORING: ok line: y = 2x + 3 Correlation:1 ->ok");
^
test3.c(243): warning #869: parameter "array" was never referenced
b = getb(sMy, sMx, sa);
^
compilation aborted for test3.c (code 2)
make: *** [test3.o] Error 2
make: Target `all' not remade because of errors.
Compilation exited abnormally with code 2 at Tue Apr 29 01:40:35
When I compile my code using the koolplot project, it does not show any error.. Any reason for that?
My values of sumxy, sumx2, sumnx2, sumnxy, sumy2, sumny2 and sumnxy2 does not return the supposed value... Can anyone check my code?
Code:/*
*This program will read a set of data from a file, calulate the correlation with the ok-line and
*draw the graph of the ok-line and plot the set of data on the graph
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <koolplot.h>
int main()
{
/* Define variables */
int j, xy, sl, yi, xl, xu, xp, p, n, sn;
double arrayx[50], arrayy[50], Mx, My, sumxy, sumx2, sumnx2, sumnxy, sumy2, sumny2, sumnxy2;
double sMx, sMy, ssumx2, ssumxy, ssumy2, ssumnx2, ssumny2, cor, scor;
/*Function Prototype*/
int readData(double arrayx[], double arrayy[]);
double getMx(double arrayx[], int n);
double getMy(double arrayy[], int n);
double getsumxy(double arrayx[], double arrayy[], double Mx, double My, int n);
double getsumx2(double arrayx[], double Mx, int n);
double getsumnx2(double sumx2, int n);
double getsumnxy(double sumxy, int n);
double getsumy2(double arrayy[], double My, int n);
double getsumny2(double sumy2, int n);
double getsumnxy2(double sumnx2, double sumny2);
void printResults(double scor);
void graph(double arrayx[], double arrayy[], int sl, int yi, int xl, int xu, int xy);
/* Read data from file */
xy = readData(arrayx, arrayy);
/*Enter details of Ok line*/
printf("Generating the ok-line\n");
printf("Enter the slope of ok line: ");
scanf("%d", &sl);
printf("Enter the y-intercept of the ok line: ");
scanf("%d", &yi);
printf("Enter lower bound of x value: ");
scanf("%d", &xl);
printf("Enter upper bound of x value: ");
scanf("%d", &xu);
printf("Enter the number of points to be generated: ");
scanf("%d", &p);
/*Store data from ok-line*/
n = xy+p;
for (j=xy; j<n; j++)
{
xp = j-xy;
arrayy[j] = sl*xp + yi;
}
for (j=xy; j<n; j++)
{
xp = j-xy;
arrayx[j] = xp;
}
/*Obtain values to calculate for correlation*/
Mx = getMx(arrayx, n);
My = getMy(arrayy, n);
sumxy = getsumxy(arrayx, arrayy, sMx, sMy, n);
sumx2 = getsumx2(arrayx, sMx, n);
sumnx2 = getsumnx2(ssumx2, n);
sumnxy = getsumnxy(ssumxy, n);
sumy2 = getsumy2(arrayy, sMy, n);
sumny2 = getsumny2(ssumy2, n);
sumnxy2 = getsumnxy2(ssumnx2, ssumny2);
/*End of values to calculate for correlation*/
/*Obtain value of cor*/
cor = sumnxy/sumnxy2;
/*Print results*/
printResults(scor);
/*Draw graph*/
graph(arrayx, arrayy, sl, yi, xl, xu, xy);
return 0;
}
/* Function readData
* Opens file and read data into marks array.
*
* Parameter: arrayx and arrayy - array of doubles to store data from file
*
* Return: number of entries read from file
*/
int readData(double arrayx[], double arrayy[])
{
char rdata[25];
int j, xy;
FILE *data;
printf("File name(must be typed with file type): ");
scanf("%s", &rdata);
/*Total pair of data from file*/
printf("Total data pair in file: ");
scanf("%d", &xy);
/* Open input file. */
data = fopen(rdata,"r");
/* Read data . */
for(j=0; j < (xy); j++)
{
fscanf(data, "%lf %lf", &arrayx[j], &arrayy[j]);
}
return xy;
}/*End of readData*/
/*Determine mean of x*/
double getMx(double arrayx[], int n)
{
double sumx1=0.00, Mx;
int j;
for(j = 0; j < n; j++)
{
sumx1 = sumx1 + arrayx[j];
}
Mx = sumx1/n;
return Mx;
}/*End mean of x*/
/*Determine mean of y*/
double getMy(double arrayy[], int n)
{
double sumy1=0.00, My;
int j;
for(j = 0; j < n; j++)
{
sumy1 = sumy1 + arrayy[j];
}
My = sumy1/n;
return My;
}/*End mean of y*/
/*Determine sumxy*/
double getsumxy(double arrayx[], double arrayy[], double Mx, double My, int n)
{
double sumxy;
int j;
sumxy = 0.00;
for(j=0; j < n; j++)
{
sumxy = sumxy + ((arrayx[j]) - Mx)*((arrayy[j]) - My);
}
return sumxy;
}/*End of sumxy*/
double getsumx2(double arrayx[], double Mx, int n)
{
/*Determine sumx2*/
double sumx2 = 0.00;
int j;
for(j = 0; j < n; j++)
{
sumx2 = sumx2 + ((arrayx[j] - Mx)*(arrayx[j] - Mx));
}
/*End of sumx2*/
return sumx2;
}
double getsumnx2(double sumx2, int n)
{
double sumnx2;
double c;
int j;
/*Determine sumnx2*/
sumnx2 = 0.00;
c = 0.00;
c = c + (sumx2/n);
sumnx2 = sumnx2 + sqrt(c);
return sumnx2;
}/*End of sumnx2*/
double getsumnxy(double sumxy, int n)
{
double sumnxy;
/*Determine sumnxy*/
sumnxy = 0.00;
sumnxy = sumnxy + (sumxy/n);
return sumnxy;
}/*End of sumnxy*/
double getsumy2(double arrayy[], double My, int n)
{
double sumy2;
int j;
/*Determine sumy2*/
sumy2 = 0.00;
for(j = 0; j < n; j++)
{
sumy2 = sumy2 + ((arrayy[j] - My)*(arrayy[j] - My));
}
return sumy2;
}/*End of sumy2*/
double getsumny2(double sumy2, int n)
{
/*Determine sumny2*/
double sumny2 = 0.00;
double d = 0.00;
d = d + (sumy2/n);
sumny2 = sumny2 + sqrt(d);
return sumny2;
}/*End of sumny2*/
double getsumnxy2(double sumnx2, double sumny2)
{
/*Determine sumnxy2*/
double sumnxy2 = 0.00;
sumnxy2 = sumnxy2 + (sumnx2 * sumny2);
return sumnxy2;
}/*End of sumnxy2*/
/* Function printResults
* Display the value of a, b, and cor
* Determine the status of chip
*
* Parameter: geta - the value of a
* Parameter: getb - the value of b
* Parameter: cor - the value of correlation
* return - void
*/
void printResults(double cor)
{
printf("Value of correlation is %.2lf\n", cor);
if (cor < 0.98)
{
printf("Possible chip failure\n");
}
else
{
printf("Chip is OK\n");
}
}
/*Function graph
*Draws graph of ok line and
*plots the data from the file on the graph*/
void graph(double arrayx[], double arrayy[], int sl, int yi, int xl, int xu, int xy)
{
int j;
plotdata x(xl, xu);
plotdata y = sl*x + yi;
for (j=0; j<xy; j++)
{
addMark(x, y, arrayx[j], arrayy[j]);
}
plot(x, y, BLACK, "SELF MONITORING: ok line: y = 2x + 3");
}