|
 |
|
 |
09-06-2008, 10:04 AM
|
#1 (permalink)
|
|
Recruit
Join Date: Sep 2008
Posts: 4
|
code Newbie Question. Seriously lol.
hey guys. wanna take this chance to say hi as well to all of you.
just took up a programming module without ANY programming knowledge and unfortunately this basic programming module is being taught by a lecturer from China. not a single soul in the lecture knows what he's mumbling about.
hoping to learn from u guys here.
We use Vi in our labs. i could use the command gcc in there but couldnt use in in Vim when im home.[ running xp]. well the lab looked like it was using windows xp as well.. weird.
Heres qn 1 which i hope u guys could guide me.
Qn 1. Write a program to approx. the derivative of f(x) = x^2 +0.5x+1 using the half-increment method, in which the derivative of f(x) at a point c is approx by [f(c+h/2) - f(c-h/2)]/h, for small h<0.1. the program takes the values of c and small h from command line and then displays the approx of the derivative of f(x) at c.
here's my coding:
Code:
#include <stdio.h>
#include <stdlib.h>
double f(double x) {
return x*x+0.5*x+1;}
int main(int argc, char *argv[])
{
double c,h,ans;
c=atof(argv[1]);
h=atof(argv[2]);
ans= (f(c+h)/2 - f(c-h)/2)/h ;
printf(" %.3f", ans);
return(0);
}
compiled it successfully. tried to open with ms-dos. gave an error. is there anything wrong with it guys? thanks a mill
and oh, can anyone tell me why we must put a double x in double f(double x)?
thx!
Last edited by redhead; 09-06-2008 at 02:44 PM.
Reason: Added [code] - [/code] tags
|
|
|
09-06-2008, 03:01 PM
|
#2 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
|
Hi Buffered, and welcome here at CN
Quote:
|
tried to open with ms-dos. gave an error.
|
You my friend have had your first encounter with the famous Unix/Dos LF/CR indifferencies
Quote:
|
can anyone tell me why we must put a double x in double f(double x)?
|
Because otherwise would return a warning telling one a conversion choice would have to be made, and depending on the compilers skills it could end up as either or which isn't the behaviour youre looking for, read about Floats vs. integers
If you want to use the same at home as you use on campus, there is mingW which provides Unix like tools for windows aswell as VI for windows
How ever, given that you most likely would delve further into teh usage of a Unix like system, I would suggest you try a Unix like environment like installing Linux under windows
|
|
|
09-07-2008, 03:56 PM
|
#3 (permalink)
|
|
Recruit
Join Date: Sep 2008
Posts: 4
|
thank you so much for ur reply! i'll be reading the links you've provided me with!
turly appreciate it! 
|
|
|
09-08-2008, 07:05 PM
|
#4 (permalink)
|
|
Code Monkey
Join Date: Aug 2008
Posts: 74
|
super easy is LaGrangian interpolation
collocation=approximation passes through given points (x(n) y(n) ) n= 0,1,2
y(x) =
y(0)* (x(1) -x) *(x(2)-x)/(x(1)-x(0)*x(2)-x(0)) + (this term = y(0) at x = x(0)
y(1)*(x(0) -x) *(x(2)-x)/(x(0)-x(1)*x(2)-x(1)) + (this term = y(1) at x = x(1)
y(2)*(x(0) -x) *(x(1)-x)/(x(0)-x(2)*x(1)-x(2)) + (this term = y(2) at x = x(2)
ends up being a parabola
|
|
|
09-09-2008, 05:21 AM
|
#5 (permalink)
|
|
Recruit
Join Date: Sep 2008
Posts: 4
|
Quote:
#include <stdio.h>
#include <string.h>
int main(int argc, char*argv[]){
int January_first_day,February_first_day,March_first_d ay,April_first_day,May_first_day,June_first_day,Ju ly_first_day,August_first_day,September_first_day, October_first_day,November_first_day,December_firs t_day;
int i,j,k,l,m,n,o,p,q,r,s,t;
printf("\n January");
printf(" Sun Mon Tue Wed Thu Fri Sat\n");
if (strcmp(argv[1], "Sun") == 0) January_first_day=6; if (strcmp(argv[1], "Mon") == 0) January_first_day=7;
if (strcmp(argv[1], "Tue") == 0) January_first_day=1; if (strcmp(argv[1], "Wed" )== 0) January_first_day=2;
if (strcmp(argv[1], "Thu") == 0) January_first_day=3; if (strcmp(argv[1], "Fri") == 0) January_first_day=4;
if (strcmp(argv[1], "Sat") == 0) January_first_day=5;
for (i = 1; i<=6*January_first_day-2; i = i+1) { printf(" ");} /* print out the first day */
printf("%2d", 1);
for (i=2; i<= 31; i=i+1) {
if ( (i+January_first_day-2)%7 == 0) printf("\n"); /* finish one week */
printf(" %2d ", i); /* insert 4 spaces for format purpose */
}
printf("\n February");
printf(" Sun Mon Tue Wed Thu Fri Sat\n");
if (strcmp(argv[1], "Sun") == 0) February_first_day=3; if (strcmp(argv[1], "Mon") == 0) February_first_day=4;
if (strcmp(argv[1], "Tue") == 0) February_first_day=5; if (strcmp(argv[1], "Wed")== 0) February_first_day=6;
if (strcmp(argv[1], "Thu") == 0) February_first_day=7; if (strcmp(argv[1], "Fri") == 0) February_first_day=1;
if (strcmp(argv[1], "Sat") == 0) February_first_day=2;
for (j = 1; j<=6*February_first_day-2; j = j+1) { printf(" ");} /* print out the first day */
printf("%2d", 1);
for (j=2; j<= 29; j=j+1) {
if ( (j+February_first_day-2)%7 == 0) printf("\n"); /* finish one week */
printf(" %2d", j); /* insert 4 spaces for format purpose */
}
|
comes out
Quote:
January Sun Mon Tue Wed Thu Fri Sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
February Sun Mon Tue Wed Thu Fri Sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
|
the 1st of every month is out of position lol. any idea why. spent the last 6 hrs. its hurting my eyes more then when im cutting onions for my mom.
thx u guys!
|
|
|
09-09-2008, 05:33 AM
|
#6 (permalink)
|
|
Recruit
Join Date: Sep 2008
Posts: 4
|
umm dunno why it showed up that way, but take it the rest of the dates except the first are out of sync with the rest, hiding away at the left corner 
|
|
|
09-09-2008, 06:13 AM
|
#7 (permalink)
|
|
Code Monkey
Join Date: Aug 2008
Posts: 74
|
try a switch statement to remove the clutter of all those strcmp
clutter can make programs unmanageable
simplicity make programs easier to debug/change
modularity
would help to add a statement stating what the code is trying to do!
pseudo code is a sort of textual flow chart
|
|
|
09-09-2008, 12:03 PM
|
#8 (permalink)
|
|
Code Monkey
Join Date: Aug 2008
Posts: 74
|
add a switch statement to eliminate those "strcmp" statements
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 03:59 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|