I need to do a programming using Xcode about free fall.I hope somebody can give me the answer.
This is the sample programming given,and using this sample as a guide,I have to answer the question.
#include <stdio.h>
int main()
{
int I;
double ip = 0.0;
double u[] = {1.9,2.8,3.7}
double v[] = {4.6,5.5,6.4}
for (i=0; i<3; i++) {
ip=ip+u[i]*v[i] ;
}
printf (“inner product=%f\n”, ip);
return 0;
}
Task1.Free Fall
Drop a ball (considered a particle) from a tall place in a free-fall. Create a program that can figure out the position of the ball each 0.1 second in the first 5 seconds after the fall.
The position of the ball after t seconds of falling is calculated by the following physics formula..
x=(1/2)gt^2
Output the results in time and corresponding position.
8 replies to this topic
#1
Posted 05 November 2011 - 10:56 AM
|
|
|
#2
Posted 05 November 2011 - 11:40 AM
I don't think you need the two arrays u and v... but I'm pretty sure you'll need to just loop a variable 'T' from 0 to 5, with increments of 0.1.
This variable will represent time.
Then just calculate x by the formula given for each iteration of your loop.
x = .5 * gravity * t^2.
print/store x.
This variable will represent time.
Then just calculate x by the formula given for each iteration of your loop.
x = .5 * gravity * t^2.
print/store x.
#3
Posted 05 November 2011 - 11:49 AM
How do I do that?I mean how do I loop it?Can you give me an example?
#4
Posted 05 November 2011 - 12:01 PM
There's a loop in the example you gave! :D :D :D
#5
Posted 05 November 2011 - 12:17 PM
That's why I was hoping someone could just write out the answer for me as I don't understand any of this :( Which part is the loop?
#6
Posted 05 November 2011 - 12:23 PM
#include <stdio.h>
int main()
{
int I;
double ip = 0.0;
double u[] = {1.9,2.8,3.7}
double v[] = {4.6,5.5,6.4}
[B]for (i=0; i<3; i++)[/B] { [COLOR="#FF0000"]// loop with variable i that starts at 0, increments by 1 until i < 3[/COLOR]
ip=ip+u[i]*v[i] ;
}
printf (“inner product=%f\n”, ip);
return 0;
}
#7
Posted 05 November 2011 - 12:42 PM
lethalwire said:
#include <stdio.h>
int main()
{
int I;
double ip = 0.0;
double u[] = {1.9,2.8,3.7}
double v[] = {4.6,5.5,6.4}
[B]for (i=0; i<3; i++)[/B] { [COLOR="#FF0000"]// loop with variable i that starts at 0, increments by 1 until i < 3[/COLOR]
ip=ip+u[i]*v[i] ;
}
printf (“inner product=%f\n”, ip);
return 0;
}Thank you for telling me that :)
Hey,what about the parts above them.I dont understand.Can you explain what does int I mean and what does double ip mean and why is it 0?
My teacher gave me some notes but it is hard to understand.
#8
Posted 05 November 2011 - 12:54 PM
It doesn't seem you need any of the "parts" above the loop. I bet your teacher is just giving you an idea about how to use variables, arrays, loops, print statements, etc.
For your task, all I see you needing is a loop and a printf statement inside of the loop.
For more help you can check out tutorials about using loops in c or c++. Your task is trivial so it shouldn't be difficult to get working and accurate results.
I'm no c/c++ guru so I couldn't tell you what language you're coding in.
For your task, all I see you needing is a loop and a printf statement inside of the loop.
For more help you can check out tutorials about using loops in c or c++. Your task is trivial so it shouldn't be difficult to get working and accurate results.
I'm no c/c++ guru so I couldn't tell you what language you're coding in.
#9
Posted 05 November 2011 - 02:26 PM
lethalwire said:
I'm no c/c++ guru so I couldn't tell you what language you're coding in.
Well, looking at this:
lethalwire said:
#include <stdio.h>
I don't have any experience with Xcode, though, so I can't give much help relating to that.
* * *
By the way,
aruwin said:
I hope somebody can give me the answer.
We don't just give out answers here, this is a forum for getting help from others, not having others do your homework.
* * *
From what I understood, you need to understand the sample you were given, before you can get the solution.
Quote
#include <stdio.h>
int main()
{
int I;
double ip = 0.0;
double u[] = {1.9,2.8,3.7}
double v[] = {4.6,5.5,6.4}
for (i=0; i<3; i++) {
ip=ip+u[i]*v[i] ;
}
printf (“inner product=%f\n”, ip);
return 0;
}So let's go over that, if you don't mind.
#include <stdio.h>C/C++ has compiler directives that tell the compiler different things, like defining constants for the program, or including another C/C++ file inside the program. Directives, in C/C++, start with the number/pound symbol ('#'). The 'include' directive "includes" the file you specify into the current program.
So let's say you have a program like this:
#include "somefile.c" // some code...
And somefile.c has this code:
// some code from some file...
Then the compiler would put things together and get this
// some code from some file... // some code..., before compiling that.
There are two kinds of includes that I have seen: <file> and "file" .
The former loads the file 'file' from one of the compiler's include directories (ie the folder where the compiler keeps 'stdio.h' , 'string.h' , and so on), while the latter loads the file 'file' from the current directory. Basically, if it's an include file that you made for the program, you would use "file" , and if it's an include file that's in the compiler's directories, where the compiler can find it, then you would use <file> .
int main()
{
This is how you define functions in C/C++. A function is a block of code that can be called from another block of code. Functions can have 0 or more input parameters, but can only return no more than one parameter. 'parameters' can also be referred to as 'arguments' .
'int' is the type of value the function returns, and 'main' is the function name. The parameter list goes inside the parentheses ('()') - in this case there aren't any parameters, so nothing goes inside the parentheses, but we do still need the parentheses, to not confuse the compiler.
The brace start after the parentheses tells the compiler to start the block of code for the function; when this function is called, this block of code is executed (run). Also, the brace start doesn't have to have its own line, you can say 'int main (){' and it would still compile correctly - it's your choice for what your style would be.
int I;
Anyone here, are variable names allowed to start from capital letters? I know function names are, so variables must also be, I think. But the variable 'i' is not defined anywhere in the code (C/C++ is case-sensitive), so I'll just pretend like the above code is actually this:
int i;
This is how you define variables in C/C++. The 'int' tells the compiler that this is an integer, and the 'i' says that the variable name is 'i' , so you can reference it later using this name. The semi-colon (';') ends the statement.
double ip = 0.0;
As you might guess, I imagine, if you think about this, this is also another definition of a variable - except this definition also assigns a value to the variable, as well as defines it. Again, the 'double' tells the compiler that this is an IEEE standard double-precision floating-point number (basically, a number with a point ('.') in it). The 'ip' tells the compiler that the name of this variable is 'ip' . The equal sign ('=') tells the compiler to assign the value on the right to the symbol/variable on the left (just like 'y=...' in algebra, if you took that class before). The '0.0' is just a number that you save inside the 'ip' variable. Again, the semi-colon (';') ends that statement.
double u[] = {1.9,2.8,3.7}
This statement is similar to the previous statement, but the difference is that the '[]' tells the compiler that 'u' is an array (or a list) of numbers with points in them. You would usually put a number inside the brackets (for example '[5]') that tells the compiler how many items are in the array (list), but I supposed that in this case the compiler can imply that number from what you give it on the right of the assignment operator ('=' sign).
The braces ('{' and '}'), in this context, tell the compiler what is inside the array (list); you separate each item with a comma. The order of the items in the list does matter.
Though I think you do need to end the statement by adding a semi-colon after the brace end (replacing '}' with '};').
double v[] = {4.6,5.5,6.4}
This is the same as the previous statement, except that the variable name is different, and the numbers are different. Again, I think you do need to end the statement.
I'm not a C/C++ expert, so I don't know for sure whether that would work, but if it doesn't compile, then try replacing the '[]' with '[3]' , to make sure the compiler gets what you mean.
for (i=0; i<3; i++) {
'for' is a type of keyword that's used for constructing loops. A loop is basically a block of code that is executed (run) until some condition is met. When you use 'for' , you must put parentheses right after it, and inside the parentheses are three statements, except you do not need to end the last statement. The first statement is what to do before starting the loop. The second statement tells whether to continue executing (running) the loop or not; this is checked each time before executing the block of code you provide, and if this evaluates to true then the loop would continue, and if it evaluates to false (0), then the loop would stop, and the code after the loop end would execute. The third statement is what gets executed after each execution of the block of code with the loop.
So in this case, the loop can be read as "start with setting i to 0; do while i is less than 3; after each time, increment i" . 'increment' means "add 1 to" , so 'i++' does the same thing as 'i= i + 1' . This helps especially when the variable name is so long that you don't want to type it two times.
The brace start ('{') tells the compiler that next is the block of code to be executed in each iteration of the loop. Each brace start must have its corresponding brace end ('}'), be it for defining functions or constructing loops.
ip=ip+u[i]*v[i] ;
Here, you just add 'u*v[i]' to 'ip' . Since you're adding something to a variable, you can just use the add-equals operator, to save on typing:
ip += u[i]*v[i] ;
Above can be read as "ip plus equals u i times v i" .
You use brackets ('[]') to reference items in an array (a list). The number between the brackets is the index into the array.
Let's say you have this array, 'a' , and it is defined like this:
char a= {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!', 0}; ('char' means "character" ; it's another [I]type, just like 'int' and 'double' .) (Each string - or text - variable must end with a 0, since that's what tells us that it's the end of the text - otherwise we wouldn't know how many characters the text has.)
a[0] would evaluate to 'H'
a[1] would evaluate to 'e'
a[7] would evaluate to 'o'
a[11] would evaluate to '!'
a[12] would evaluate to 0
(By the way, you can treat characters as numbers, just like integers or floating-point numbers; you can add them together, add integers to them, etc. That's because they're represented as numbers, in memory; the way programs know what character it is is that each character is assigned an ASCII number: '0' is 48, '1' is 49, '2' is 50, etc. Also, 'A' is 65, 'B' is 66, and so on; 'a' is 97, 'b' is 98, ... On Windows, a lot of the alt codes are based on ASCII numbers.)
}
This just tells the compiler that it's the end of the current block of code.
printf (“inner product=%f\n”, ip);
'printf' is a function that is defined inside the 'stdio.h' file (the one that you included earlier). You call a function by typing its name, followed by parentheses with the list of arguments to pass to it inside. So in this case parameter 1 is the memory address of the string (text) "inner product=%f\n" (if you're on Windows you would actually replace "\n" with "\r\n" , but for Linux and Macintosh "\n" is fine).
'printf' is a somewhat weird function, because the second, third, fourth, ..., parameters depend on the first parameter. The "%f" tells 'printf' that the next parameter is a floating-point number. Before the text is printed to the screen, the "%f" is replaced with the actual number that's passed to it as the next parameter.
return 0; }
The 'return' keyword tells the compiler to return from (finish executing) the current function. This function is supposed to return an integer (hence the 'int' before the 'main' , when the function was defined), so we put a number after the 'return' keyword, saying that this is what we want to return. It is also possible to return a number from a variable, that is of the type 'int' . If it's of another type, you need to use type casting, which I wouldn't get to right now, as this post is already long enough.
The '}' tells the compiler that it's the end of the function's code block.
So if you know this stuff, and you know what the process is of doing what you need to do, then you can put what you need to do into code, and have the computer do it for you (that's the nice thing about programming :) ).
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









