LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-17-2009, 07:45 AM   #1
SuperNomad
LQ Newbie
 
Registered: Feb 2009
Posts: 7

Rep: Reputation: 0
'C' error in printf warning: format ‘%lf’ expects type ‘double’ ...


I'm writing a program to plot a set of cosine and sine functions, so I want to output the calculated values to a file so I can plot them with gnuplot.

Here is the code:

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

//#define PI 3.14159;

int main() {
double a1;
double x[100];
double l = 20.0;
double result[100];
int a2, n = 11;
double arg;
double PI = 3.14159;
FILE *f;
//double *ptr =
int i =0;
int j = 0;
f = fopen("output.txt", "w");
fprintf(f, "n x value");

while(n <= 100) {
a1 = n; /* To get the program to do 2 different operations for odd and even numbers
we declare 2 variables as an int and a double so when we divide them
by 2 if the value is even then a1 and a2 will be equal as integer division will
return an integer and will round and non-integer values */
a2 = n;
a1 = a1/2;
a2 = a2/2;
printf("a1 = %lf\n a2=%d\n", a1, a2);
if(a1 == a2) {
//n is even so do sine
while (i!=100) {
arg = ( ( (x[i]) * (n) * (PI) ) / (l) );
result[i] = sin(arg);
fprintf(f, "%d %lf %lf\n", n, x, result[i]); //This is the error
x[i] = x[i] +0.2;
i++;
}
}

else {

//n is odd so do cosine
while(j<=100) {
arg = ( ( (x[i]) * (n) * (PI) ) / (l) );
result[i] = cos(arg);
x[i] = x[i] +0.2;
j++;
}


}
n++;
}

fclose(f);

n++;
}

However it gets an error on line 35 - the fprintf function.

sine_and_cosine_2.c: In function ‘main’:
sine_and_cosine_2.c:35: warning: format ‘%lf’ expects type ‘double’, but argument 4 has type ‘double *’

The thing is if I try and compile this on the my universitys linux server it compiles fine as have all the times I've tried to print an array element, it's only on my laptop which runs Ubuntu 9.10 with the GCC compiler. The compiler code I use is:

gcc sine_and_cosine_2.c -lm -o sine_and_cosine_2

I'm a little bit baffled, as always help would be greatly appreciated.
 
Old 11-17-2009, 08:01 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Use code tags next time, so your code will be readable.

Code:
  double x[100];
Code:
      fprintf(f, "%d %lf %lf\n", n, x, result[i]); //This is the error
probably you meant x[i] not x as the 4'th arg to fprintf.

Quote:
The thing is if I try and compile this on the my universitys linux server it compiles fine as have all the times I've tried to print an array element, it's only on my laptop which runs Ubuntu 9.10 with the GCC compiler.
Aren't you lucky to have a better compiler on that Ubuntu system!

On all the others it might compile without error, but it would run incorrectly.
 
Old 11-17-2009, 08:40 AM   #3
SuperNomad
LQ Newbie
 
Registered: Feb 2009
Posts: 7

Original Poster
Rep: Reputation: 0
woot that worked, thanks for the help.

Just out of interest when it says 4th argument I thought it meant result[i] because 'f' would be the first, n the 2nd, x the 3rd and result[i] the 4th.
 
Old 11-17-2009, 09:03 AM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by SuperNomad View Post
Just out of interest when it says 4th argument I thought it meant result[i] because 'f' would be the first, n the 2nd
"%d %lf %lf\n" is the 2nd
 
Old 11-17-2009, 09:07 AM   #5
SuperNomad
LQ Newbie
 
Registered: Feb 2009
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks again!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What is type size_t's format in printf? duyuyang Linux - Newbie 7 07-20-2011 01:04 AM
Minus sign in printf format string doesn't work. zebela Linux - Newbie 4 06-13-2008 01:32 AM
Warning ___ specifies undefined mime type/service type mbvpixies78 Linux - Software 0 12-29-2007 10:25 PM
Is there a currency format with printf() ? LUB997 Programming 1 01-03-2005 08:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration