Computing.Net > Forums > Programming > Help with a velocity C program.

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Help with a velocity C program.

Reply to Message Icon

Name: Mys7iCaL
Date: November 8, 2006 at 17:07:15 Pacific
OS: Win XP
CPU/Ram: 2.6 ghz, 1024 mb ram
Comment:

Hi I'm having trouble with creating a program where I could enter 6 different distances and 6 different times (in mins) and make a programe display 6 different velocities.

Here is what I got so far but I think there may be too many errors.

/* Velocity.C: Calculate her average velocity for her runs for 6 days a week. */

#include <stdio.h>

Main ()
{
float t, d, v;
float d_array[] = { 5, 5, 7, 4, 3, 4 };
float t_array[] = { 35, 42, 37, 40, 39, 33 };
float time ();
printf( "Mon /t Tue /t Wed /t Thu /t Fri /t Sat /n" )
for { d = 0; d < 5; d++ )
{
for ( t = 0; t < 5; t++ )
{
v = d_array[d] =/ t_array[t];
printf( "%d /t", v );
};
}; return 0;
}

float time(t)
{
for ( t = 0; t < 5; t++ )
{ t_array[t] = t_array[t] =/ 60 };

}

I'm using a c++ writing program for it though.



Sponsored Link
Ads by Google

Response Number 1
Name: Guy
Date: November 8, 2006 at 18:31:13 Pacific
Reply:

LOL. Try:

-lower case main
-not redefining the time() function

that shoud get you part way there.

Guy


0

Response Number 2
Name: Mys7iCaL
Date: November 8, 2006 at 19:18:30 Pacific
Reply:

Ok ive done a bit more but I get 3 errors.
An expression syntax at the function of time, a function should return a value at the time function and "parameter t_array is never used."


#include <stdio.h>

float time ( float t, float t_array[] );


int main ()
{
float t, d, v;
float d_array[] = { 5, 5, 7, 4, 3, 4 };
float t_array[] = { 35, 42, 37, 40, 39, 33 };
time ( t, t_array);
printf( "Mon /t Tue /t Wed /t Thu /t Fri /t Sat /n" );
for ( d = 0; d < 5; d++ )
{
for ( t = 0; t < 5; t++ )
{
v = d_array[d] /= t_array[t];
printf( "%f /t", v );
};
}; return 0;
}

float time( float t, float t_array )
{
for ( t = 0; t < 5; t++ )
{ t_array[] = t_array[t] =/ 60;
};
}


0

Response Number 3
Name: Fozzie
Date: November 9, 2006 at 16:10:11 Pacific
Reply:

In most languages time is already defined as a function or is a reserved word. Use a different name for your function.

You declare that your function returns a float value but it doesn't return anything. If you want to change the values stored in your array inside your function you would have to pass it to your function by reference, but I really think you should rethink the flow of your program as passing arrays into a function by reference really shouldn't be neccessary.

This line:

t_array[] = t_array[t] =/ 60;

...is not going to work because you didn't specify which element of your array should have the value of t_array[t]=/60 assigned to it.


0

Response Number 4
Name: Mys7iCaL
Date: November 9, 2006 at 18:55:23 Pacific
Reply:

Ok, could anyone please give me help with making two arrays of numbers (asking for input for the numbers would be nice) and then getting one of the arrays and dividing it by 60 as well as divinding by the other array, then printing the 6 answers in lines.



0

Response Number 5
Name: Fozzie
Date: November 9, 2006 at 20:35:11 Pacific
Reply:

We are trying to help you. You obviously have some C programming knowledge and you have in the code that you posted all of the tools necessary to make your program except for the input.

I recommend simplifying. You don't need to bother with creating your own function. Just perform your mathematical operations inside the loop.


0

Related Posts

See More



Response Number 6
Name: Mys7iCaL
Date: November 9, 2006 at 20:36:12 Pacific
Reply:

Ok I just remade the program, and I think it's easier to understand what I'm trying to do now. Can someone please help with syntax and logical errors (if there are any)

#include <stdio.h>

float velocity ( float t_array[], float d_array[], float veloc );

main ()
{
float t_array[] = { 1, 2, 3, 4, 5, 6 };
float d_array[] = { 6, 5, 4, 3, 2, 1 };
float veloc;
velocity ( float t_array[], float d_array[], float veloc );
return 0;
}

float velocity (float t_array[], float d_array[], float veloc );
{
float veloc;

for ( t = 0; t < 6; t++ )
{
veloc = t_array[t] % d_array[t] % 60;
printf ( "%d", veloc);
};
return 0;
}


0

Response Number 7
Name: Fozzie
Date: November 10, 2006 at 18:27:39 Pacific
Reply:

You're getting closer.


float velocity (float t_array[], float d_array[], float veloc );
{
float veloc;

for ( t = 0; t < 6; t++ )
{
veloc = t_array[t] % d_array[t] % 60;
printf ( "%d", veloc);
};
return 0;
}

In this function, you are passing values in and printing out the results before exiting the function which is good, but you are not returning a meaningful value. In fact you don't need to return a value at all. You can re-write your function as

VOID velocity(etc..........

and then use return without a value.

Your variable veloc is not used in main and does not need to be declared there or passed into your velocity function. You should drop that parameter from your function header. (Don't forget to remove it in your function declaration as well.)

If you do these things then I believe your program will compile and run. If it doesn't, post your revised code and any errors you receive and I will try to point you in the right direction.

Don't give up! You're getting there.


0

Response Number 8
Name: Mys7iCaL
Date: November 10, 2006 at 21:13:19 Pacific
Reply:

Thanks :) I've solved the first of my problems. Now I need to know how to ask for input for the values in the arrays.

This is my program so far and it's much more simplified;

#include <stdio.h>


main ()
{
float d_array[] = { 5, 4, 5, 5, 5, 5 };
float t_array[] = { 18.7, 17.3, 18.3, 20, 18.8, 19.3 };
float veloc;
int t;
for ( t = 0; t < 6; t++ )
{
veloc = d_array[t] / (t_array[t] / 60);
printf ( "day%d = %2.3fkm/h \n",t, veloc);
};
return 0;
}


0

Response Number 9
Name: Fozzie
Date: November 11, 2006 at 18:31:11 Pacific
Reply:

I knew you could do it.

To get input from a user you can use fscanf, or you could use the cin function in the iostream.h header.


0

Response Number 10
Name: Mys7iCaL
Date: November 11, 2006 at 20:54:27 Pacific
Reply:

Can you please use an example of fscanf for me? Sorry I haven't dealth with input before.


0

Response Number 11
Name: Fozzie
Date: November 11, 2006 at 22:48:21 Pacific
Reply:

Oops! Should have been scanf.

http://www.cplusplus.com/ref/cstdio...


0

Response Number 12
Name: Mys7iCaL
Date: November 12, 2006 at 21:15:23 Pacific
Reply:

Ok I get scanf... I just don't know how to use it in an array :s


0

Response Number 13
Name: Fozzie
Date: November 13, 2006 at 20:54:35 Pacific
Reply:

You do know how, you just don't realize it.

float d_array[6]; //creates an array with 6 empty slots


Break it down into steps:

Use scanf to get input.

Assign the inputted value to the appropriate element of the array.

Hmmm.. maybe some sort of loop would help this go smoother.


0

Response Number 14
Name: Mys7iCaL
Date: November 14, 2006 at 00:24:17 Pacific
Reply:

So would something like this work for distance?

for ( f = 0; f < 6; f++ )
{
printf( "Enter distance %d", f );
scanf ("%2.3f", d_array[f]);
};


0

Response Number 15
Name: Fozzie
Date: November 14, 2006 at 18:16:39 Pacific
Reply:

Yes.

scanf("%l", d_array[f]);

"%d" allows input of an integer.
"%l" allows a floating point number.


0

Response Number 16
Name: Mys7iCaL
Date: November 14, 2006 at 23:03:40 Pacific
Reply:

It doesn't work :(
Here is my code for it, could someone please tell me what's wrong?

#include <stdio.h>


main ()
{
printf ("Enter 6 distances and 6 times to find your velocities for 6 days");
float d_array[6];
float t_array[6];
float veloc;
int g,f,t;
for ( f = 0; f < 6; f++ )
{
printf( "Enter distance %d", f);
scanf( "%l", &d_array[f] );
}
for ( g = 0; g < 6; g++ )
{
printf( "Enter time %d", f);
scanf( "%l", &t_array[t] );
}

for ( t = 0; t < 6; t++ )
{
veloc = d_array[t] / (t_array[t] / 60);
printf ( "day%d = %2.3fkm/h \n",t, veloc);
};
return 0;
}


0

Response Number 17
Name: Mys7iCaL
Date: November 14, 2006 at 23:43:58 Pacific
Reply:

Yay it works :D Thanks for the help fozzie!


0

Response Number 18
Name: Fozzie
Date: November 15, 2006 at 17:14:39 Pacific
Reply:

That's great! You're welcome.



0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Help with a velocity C program.

I need help with a short C++ program please!! www.computing.net/answers/programming/i-need-help-with-a-short-c-program-please/20280.html

Help with a C Program / Newbie www.computing.net/answers/programming/help-with-a-c-program-newbie/8071.html

help with C www.computing.net/answers/programming/help-with-c-/7817.html