LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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
 
LinkBack Search this Thread
Old 11-17-2009, 08: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, 09:01 AM   #2
johnsfine
Senior Member
 
Registered: Dec 2007
Distribution: Mepis, Centos
Posts: 3,837

Rep: Reputation: 685Reputation: 685Reputation: 685Reputation: 685Reputation: 685Reputation: 685
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, 09: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, 10:03 AM   #4
johnsfine
Senior Member
 
Registered: Dec 2007
Distribution: Mepis, Centos
Posts: 3,837

Rep: Reputation: 685Reputation: 685Reputation: 685Reputation: 685Reputation: 685Reputation: 685
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, 10:07 AM   #5
SuperNomad
LQ Newbie
 
Registered: Feb 2009
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks again!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are 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 02:04 AM
Minus sign in printf format string doesn't work. zebela Linux - Newbie 4 06-13-2008 02:32 AM
Warning ___ specifies undefined mime type/service type mbvpixies78 Linux - Software 0 12-31-2007 07:44 AM
Is there a currency format with printf() ? LUB997 Programming 1 01-03-2005 09:33 PM


All times are GMT -5. The time now is 09:20 AM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration