Jump to content
Sign in to follow this  
Ex-RoNiN

Anybody know "C"?

Recommended Posts

Ok, I have a coursework assignment in C. Write a proggie that does the Trapezium rule using x and y co-ordinates. Fairly straightforward....BUT:

i need to use arrays to input the x and y values of course. i will need to do this in a "for" loop to allow for multiple values. then i need to calculate the area for each trapezium and sum all these areas. How do I do it? Any hints/ideas/tips? Because I have no idea how to do this, apparently the array subscripts can not be variables wow.gifconfused.gifmad.gif

Share this post


Link to post
Share on other sites

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (Ruskie @ Feb. 16 2002,17:40)</td></tr><tr><td id="QUOTE">i have no idea what ''C'' is sad.gif<span id='postcolor'>

its a programming language tounge.gif

Share this post


Link to post
Share on other sites

A trapezium is a regular polygon, i think. And why do you have to do it in C not C++ or something else. C suckz

Share this post


Link to post
Share on other sites

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (Damage Inc @ Feb. 16 2002,17:42)</td></tr><tr><td id="QUOTE">C is a programming language. But WTF is a trapezium confused.gif<span id='postcolor'>

The "Trapezium Rule" is an approximation technique for integration, or finding the area under a curve. What I basically have to do is type in values for x1 and x2, y1 and y2. x1 and x2 are on the x-axis (duh) and the y-values are points on the curve. Because y1 and y2 are going to be different, i have to find the average y-value and calculate the area as if it was a rectangle. That is not the problem really. The big problem is to read in the y and x values in that darned "for" loop sad.gif

Share this post


Link to post
Share on other sites

the c is a basic informatic language , i think a lot of games are programmed in C++, am i wrong ?

Share this post


Link to post
Share on other sites

Yeah, i should be able to help, after a A level in maths, but i have no idea how to do it in C. Sory, try a programming comunity anyway.

Share this post


Link to post
Share on other sites

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (Epita @ Feb. 16 2002,17:47)</td></tr><tr><td id="QUOTE">Yeah, i should be able to help, after a A level in maths, but i have no idea how to do it in C.  Sory, try a programming comunity anyway.<span id='postcolor'>

Do you know some?

Share this post


Link to post
Share on other sites

Umm no. Just go to google and serch for programming communities, im sure u'll find one that has the anser. Hopefully.

Share this post


Link to post
Share on other sites

ok, seems like we have a start:

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">

struct coord { int x, y; };

struct coord myvalues[MAX_VALUES];

for (i = 0; i < MAX_VALUES; i++)

{ printf("Enter an x and y values?"); scanf("%i %i", &myvalues.x, &myvalues.y);

}

<span id='postcolor'>

anyone would like to explain to me what is happening there?

Share this post


Link to post
Share on other sites

ok, this prob is solved, ill be back soon with pesky details smile.gif

Share this post


Link to post
Share on other sites

ahh,, visiting the beautifull heaven of programming? Nice smile.gif

Alltough it's a Game development community, you should definetly check out GameDev.net. This is a great site which cover allot of programming issues, tips and allot more. Visit the forums smile.gif

Share this post


Link to post
Share on other sites

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (Snake1999 @ Feb. 16 2002,20:08)</td></tr><tr><td id="QUOTE">ahh,, visiting the beautifull heaven of programming? Nice smile.gif

Alltough it's a Game development community, you should definetly check out GameDev.net. This is a great site which cover allot of programming issues, tips and allot more. Visit the forums smile.gif<span id='postcolor'>

that is an EXCELLENT site! Snake, u r the best biggrin.gif

Share this post


Link to post
Share on other sites

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote </td></tr><tr><td id="QUOTE">ok, seems like we have  a start:

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">

struct coord { int x, y; };

struct coord myvalues[MAX_VALUES];

for (i = 0; i < MAX_VALUES; i++)

{ printf("Enter an x and y values?"); scanf("%i %i", &myvalues.x, &myvalues.y);

}

<span id='postcolor'>

anyone would like to explain to me what is happening there?<span id='postcolor'>

uhm, I happen to know a bit about c, but more about c++.

a struct is a collection of data.

if you write:

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">

struct mytype

{

 int data;

 int other_data;

}

<span id='postcolor'>

then "data" and "other_data" are members of a datatype named "mytype".

Then you can use your struct with the preceeding keyword "struct" ( in c++ you can skip that ) and make an instance like with every other datatype.

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">

struct mytype myinstance;

<span id='postcolor'>

the "[" and "]" are array accessors. if you write them after the name of your instanced datatype, you get an array of the size of the number enclosed by thos brackets.

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">

int myarray[100];

<span id='postcolor'>

if you want to access the single values in the array, you specify in the same manner the position you want to access.

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">

myarray[23] = 1234;

<span id='postcolor'>

the "." is the member accessor for structs. If you write:

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">

mytype.data;

<span id='postcolor'>

You do access the data member of your struct.

the "for" construct takes up to three commands that are executed handy within round brackets, and limited by semicolons. Any condition enclosed by the middle command is the condition to jump out of the loop. The first command is only executed once at the beginning, and the last is executed every loop.

"printf" takes a char array as argument and prints it on the screen.

"scanf" takes as first argument a char array where all the used specifikations about the input format are made. Every other argument is a a reference to the variable you want to prompt from the user.

I hope this has cleared the thing a bit. And seriously, you should use c++, cause there the code is much easier.

For example it would look like:

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">

#include <iostream>

using namespace std;

struct coord

{

 int x, y;

}

void main()

{

 coord values[MAX_VALUES];

 for( int i=0; i<MAX_VALUES;i++ )

 {

   cout << "Enter an x and y values?";

   cin >> values.x;

   cin >> values.y;

 }

}

<span id='postcolor'>

Share this post


Link to post
Share on other sites

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (Ex-RoNiN @ Feb. 16 2002,17:43)</td></tr><tr><td id="QUOTE"></span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote </td></tr><tr><td id="QUOTE">i have no idea what ''C'' is sad.gif<span id='postcolor'>

its a programming language tounge.gif<span id='postcolor'>

I personally like to call it IT tounge.gif

Share this post


Link to post
Share on other sites

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (Ruskie @ Feb. 16 2002,20:46)</td></tr><tr><td id="QUOTE"></span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote </td></tr><tr><td id="QUOTE"></span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote </td></tr><tr><td id="QUOTE">i have no idea what ''C'' is sad.gif<span id='postcolor'>

its a programming language tounge.gif<span id='postcolor'>

I personally like to call it IT tounge.gif<span id='postcolor'>

and what is c++?

ITit? tounge.gif

Share this post


Link to post
Share on other sites

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (Thorn @ Feb. 16 2002,20:39)</td></tr><tr><td id="QUOTE"></span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote </td></tr><tr><td id="QUOTE">ok, seems like we have  a start:

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">

struct coord { int x, y; };

struct coord myvalues[MAX_VALUES];

for (i = 0; i < MAX_VALUES; i++)

{ printf("Enter an x and y values?"); scanf("%i %i", &myvalues.x, &myvalues.y);

}

<span id='postcolor'>

anyone would like to explain to me what is happening there?<span id='postcolor'>

uhm, I happen to know a bit about c, but more about c++.

a struct is a collection of data.

if you write:

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">

struct mytype

{

 int data;

 int other_data;

}

<span id='postcolor'>

then "data" and "other_data" are members of a datatype named "mytype".

Then you can use your struct with the preceeding keyword "struct" ( in c++ you can skip that ) and make an instance like with every other datatype.

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">

struct mytype myinstance;

<span id='postcolor'>

the "[" and "]" are array accessors. if you write them after the name of your instanced datatype, you get an array of the size of the number enclosed by thos brackets.

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">

int myarray[100];

<span id='postcolor'>

if you want to access the single values in the array, you specify in the same manner the position you want to access.

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">

myarray[23] = 1234;

<span id='postcolor'>

the "." is the member accessor for structs. If you write:

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">

mytype.data;

<span id='postcolor'>

You do access the data member of your struct.

the "for" construct takes up to three commands that are executed handy within round brackets, and limited by semicolons. Any condition enclosed by the middle command is the condition to jump out of the loop. The first command is only executed once at the beginning, and the last is executed every loop.

"printf" takes a char array as argument and prints it on the screen.

"scanf" takes as first argument a char array where all the used specifikations about the input format are made. Every other argument is a a reference to the variable you want to prompt from the user.

I hope this has cleared the thing a bit. And seriously, you should use c++, cause there the code is much easier.

For example it would look like:

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">

#include <iostream>

using namespace std;

struct coord

{

 int x, y;

}

void main()

{

 coord values[MAX_VALUES];

 for( int i=0; i<MAX_VALUES;i++ )

 {

   cout << "Enter an x and y values?";

   cin >> values.x;

   cin >> values.y;

 }

}

<span id='postcolor'><span id='postcolor'>

lol! ok, i have had that programming class for about a semester and a half - that much detail wasn't required tounge.gif (although i do appreciate it wink.gif )

my main problem was with the structure thing, as i hadnt done that before. as for c++, well, lecturer said no, this year we only do C sad.gif

Share this post


Link to post
Share on other sites

uhm, tell your lecturer he does not know jack, and he should better get going to learn some serious languages.

c you will pick up on the way learning c++. Important is only to be aware which syntax actually is c and which is c++. As for learning to programm, nearly nobody does prefer c today for anything if he has the choice to start from scratch. It is still much used in the operating programming sector, because the time most of the OS begun to took modern shape, c was hip.

Today c++ is almost "old school". And if you want seriously do some speed work, you take assembler or forth, but I do assume that you do not want to do micropsocessor programming in general, so c++ will serve you well.

Share this post


Link to post
Share on other sites

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote </td></tr><tr><td id="QUOTE"></span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote </td></tr><tr><td id="QUOTE"></span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote </td></tr><tr><td id="QUOTE"></span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote </td></tr><tr><td id="QUOTE">i have no idea what ''C'' is sad.gif<span id='postcolor'>

its a programming language tounge.gif<span id='postcolor'>

I personally like to call it IT tounge.gif<span id='postcolor'>

and what is c++?

ITit?<span id='postcolor'>

ermmm, advanced IT?

tounge.gif

Share this post


Link to post
Share on other sites

the structure thingy is in c the greatest feature when it came out. Now it is in c++ only the tip of the iceberg.

The handling of objects in c is booring. They can nothing else then represent collections of data, and that's it. In c++ you can have routines called upon construction of your instance, upon destruction. Upon use with operators you can have your routines running, and you can give it own routines, accessed like members. You can even pass arguments for construction of your instance.

I can hear your lecturer call, ohno OO. And for some part he is right, if you programm without passion, in a rather hacking manner, like you would do to hack down pure procedural code, you make bad OO code. To make good OO code is a manner of passion, and awareness of what you do. For OO can produce the much nicer and simpler solutions for problems then the procedural aproach can, and will therefore be superior in the long term. It has of course a dark side, OO is a sharp blade, and you will more likely fall from your way of fast code. Maybe you have heard the saying "It's easy to make things complex, but its hard to make them simple". And OO helps you with that. And do never forget that tweaking and tricksing about code is fine and all, but if you write bad algorithms, not the best c code can save you from suffering runtime loss. Writing smart algorithms is THE thing you can always improove, and the better you get, the faster your code will run.

Share this post


Link to post
Share on other sites

Ex-Ronin:

Probably Structure and Classes will be the hardest part of C++ experience. For me rest after that was easy. So work hard, and you'll get the rest of basic concepts easy after this uphill.

Share this post


Link to post
Share on other sites

I'm a bit late but does this help? I hate calculas & i couldn't imagine it in programming. I get about programming in simple visual basic(that's all i need really).

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×