# College project on arrays and functions

• 12-03-2011
jthunder
College project on arrays and functions
First post here

Im a computer science major and my c programming class was assigned a project and i was wondering if someone could give me a push. its on arrays and functions. We use K.N. King's c programming book. I understand everything so far except probably arrays. Let me also say I really want to learn this stuff and im not interested in a free ride but I would like some input from you guys. I appreciate any help.

Basically these are the directions

Coach Whytlaw, the local swim coach has a problem he wants you to help solve.
The timing system at the pool gives him a le containing information about
each race. The le contains:
The number of races in the le, on a line by itself.
For each race, there is a line in the le containing the following:
Information about the pool as follows: Whether the Pool is Long corse (L)
or Short Corse (S) Whether the Pool is Meters (M) or Yards (Y)
Information about the length of the race, in appropriate units. The shortest
race is 50 yards or meters and the longest race is 1650 yards or meters.
The time (in seconds) for each length of the pool (A length of the pool in a
long corse pool is 50 meters or yards. A length of the pool in a short corse pool
is 25 meters or yards).
The coach would like you to use the times to do some analysis on the races.
Here is the information he would like:
 The total length of time it took to swim the race (displayed in minutes
and seconds).
 The time for each 50 yards in the race (displayed in minutes and seconds).
 For races longer than 100 yards or meters, the time of each multiple of
100 yards or meters.
 The fastest 50 yards or meters, by number (1st 50, 2nd 50,etc)
 the slowest 50 yards or meters, by number (1st 50, 2nd 50,etc)
 the fastest 100 yards or meters, by number (1st 100, 2nd 100,etc)
 the slowest 100 yards or meters, by number (1st 100, 2nd 100,etc)
 Wheter or not the race was a negative split (was the rst half of the race
slower than the second half ).
• 12-03-2011
Welcome to the forum, JThunder! :cool:

However, we don't do what you're asking for -- yet.

It's totally up to YOU to give your assignment a start. When you get stuck on some programming issue on C or pseudo code, then post what you have done so we know where you're at with it, and tell us what is the problem(s), specifically.

What you've posted here, is WAY too broad, and there's no intelligent way to answer what it asks, without basically doing it.

So, get started, and when you get stumped, even if it's just a skeleton of a program or pseudo code, post it back when you have questions/problems.

For a general start on these things, I like to put off all the details that are not essential, and work on the basic flow of the program, first. Get the functions laid out even though they're just a stub function, atm.

Then take the easiest requirement, and do it first, check it, then next easiest, etc.

Last thing I do is add any details I left out previously.
• 12-03-2011
jthunder
Thanks for the reply. That sounds good man, ill get started and i appreciate the tips. ill be sure to post back in this thread.
• 12-03-2011
dmh2000
if you write out by hand two things, following exactly what the instructions say
1. what the data in the file would look like
2. what steps you would do to figure out the answers to the questions (without a computer)
3. translate that stuff to a data file and code (with the results of #2 as comments)
• 12-04-2011
Strahd
If you are having issues with arrays, remember you ARE NOT limited to that book the professor assigned. There is always the internet, and other books, you can use.

But take a look at Arrays in C - Cprogramming.com (Lesson 8: Arrays in C).
• 12-04-2011
jthunder
Thanks a lot guys. I am starting to work on it again now. Its due tomorrow at midnight so hopefully ill cover a lot of ground today. Much appreciated
• 12-04-2011
jthunder
Thanks for posting the link to that chapter on arrays! very helpful. I'm thinking I'm going to have to scan the values into the array and use them to answer the questions. If I have that correct I think this project will be easier than I thought.

Quick question, lets say an array has 4 slots (for four times of the race) can I scan in four values to store into that array ? Silly question maybe lol but I think with that I can finally begin this project
• 12-04-2011
Alan Gott
Quote:

Originally Posted by jthunder
Thanks for posting the link to that chapter on arrays! very helpful. I'm thinking I'm going to have to scan the values into the array and use them to answer the questions. If I have that correct I think this project will be easier than I thought.

Quick question, lets say an array has 4 slots (for four times of the race) can I scan in four values to store into that array ? Silly question maybe lol but I think with that I can finally begin this project

Yep, each 'slot' of the array can hold a value. (Excluding strings which need a 'slot' per character.)
• 12-04-2011
jthunder
Great. I'll get started now and post later. Thanks
• 12-04-2011
jthunder
Thanks to all who have helped so far. I actually am almost finished. My question is how would one find the largest and smallest value in an array, when the values in the array have been entered in by the user
?
I plan to compute this using 3 functions, one for each race.

Code:

```/*Function to compute info for first race*/ float longcourse (float lcm[]) {   /*INTENTIONALLY LEFT BLANK FOR NOW FASTEST 50*/     /*SLOWEST TIME GOES HERE SLOWEST 50*/ /*FASTEST 100*/ /*SLOWEST 100*/ /*NEGATIVE SPLIT*/ return 0; } /*Function to computer info for second race*/ float shortcourse1 (float scm[]) { return 0; } /*Function to compute info for third race*/ float shortcourse2 (float scy[]) { return 0; }```
• 12-04-2011
jthunder
This is what I ended doing for the fastest 50. Maybe inefficient..what do you think? Im sure there is a better way but it works..

Code:

```/*Function to compute info for first race*/ float longcourse (float lcm[]) {   /*INTENTIONALLY LEFT BLANK FOR NOW FASTEST 50*/  if (lcm[0] > lcm[1] && lcm[0] > lcm[2] && lcm[0] > lcm[3])   return  printf("Fastest 50: %.2f\n", lcm[0]);  else if(lcm[1] > lcm[0] && lcm[1] > lcm[2] && lcm[1] > lcm[3])   return printf("Fastest 50: %.2f\n", lcm[1]); else if (lcm[2] > lcm[1] && lcm[2] > lcm[0] && lcm[2] > lcm[3])   return printf("Fastest 50: %.2f\n", lcm[2]); else if (lcm[3] > lcm[0] && lcm[3] > lcm[1] && lcm[3] > lcm[2])   return printf("Fastest 50: %.2f\n", lcm[3]);```
• 12-04-2011
CommonTater
You had better check what the return value of printf() is... your function is going to return garbage.

Look it up in your compilers library documentation ... if you don't have it... get it.
• 12-04-2011
jthunder
I got this in compile time warnings

warning C4244: 'return' : conversion from 'int' to 'float', possible loss of data

it works but i get a lot of those warnings
• 12-04-2011
You would probably just use a min and max variable. They are both assigned the first value entered. Then for every value entered thereafter, you do two if statements:
Code:

```for(each value entered) {   //any other code here   if(Value is the first Value) {       min = Value;       max = Value;   }   else {       if(newValue > max)         max = newValue;       if(newValue < min)         min = newValue;   } }```
For maximum speed, you'd want to assign min and max variables to the first value of the array[0] (or entered value from the user), outside the for or while loop, and just start the array index from 1, instead of 0, and keep the "else" code only, inside the loop.

No "else" is kept in that case, of course.
• 12-04-2011
CommonTater
Quote:

Originally Posted by jthunder
I got this in compile time warnings

warning C4244: 'return' : conversion from 'int' to 'float', possible loss of data

it works but i get a lot of those warnings

Because you are not paying attention to types, return values and other necessary things.

Don't ignore warnings... fix them.

Like I said... get the documentation and look this stuff up...