Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.

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;
};
}

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.

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.

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.

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;
}

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.

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;
}

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.

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.

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]);
};

Yes.
scanf("%l", d_array[f]);
"%d" allows input of an integer.
"%l" allows a floating point number.

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;
}

![]() |
![]() |
![]() |

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