0

I'm having some problems with this excercise. The way this program works is the user enters in the number of data sets so lets say 3. Then the user enters in the number of floats then the floats and then presses enter for the second and third data sets. So an example input would be.

Enter the number of data sets: 3
3 12.5 3.4 6.7
5 7.7 5.5 8.8 3.3 5.7
2 89.3 84.5

After that the user picks what data to perform an operation on let's say data 2 which is the [5 7.7 5.5 8.8 3.3 5.7] and then return the max of the numbers.

Since we don't know what the number of data sets we have to use a dynamically allocated array which I did in my code, and have pointers point to that array to find the max of that specific data set. I think I need two arrays one to keep track of the data, and one to keep track of the sets. Here is my code.

Im having trouble in the gettingData method I want the users inputs to be put in that array, but when I print what the pointer points to it returns 0.0000 which isn't accurate.

  • Please, send the code you have used to test, or something about we can do something. See how to make questions in the standard StackOverflow documentation page. Not doing could trigger the "not a programming question" flag and your question will be closed. – Luis Colorado Feb 13 '17 at 09:55

2 Answers2

0

You seem to forget one thing: before you attempt to read any variable in your code, it must be initialized. Generally, initialization can be achieved by two kinds of instructions:

  • assignment instruction, i.e. a = expression. Note, that in order to assign a value to a you must first obtain the value of expression, so any variables used in expression must be initialized first.
  • passing the address (&a) of a variable to a function, which puts a value under the given address (which is done by writing *a = expr). As before, expr cannot use any uninitialized variables.

What is more, your program does not seem to have any instruction which reads floats from input.

What is more, remember to free memory allocated by malloc when you'r done with the data! If you'r interested why, please read this thread.

As @Aubin said, you have passed not enough arguments to gettingData function.

Important note: When using a good compiler (ex. gcc, clang), it's always a good idea to use -Wall flag, which spots simple mistakes like these, and warns you even before you have a chance to run the code. Mistakes like those are done by programmers through their whole life, and -Wall flag saves a lot of time to the whole mankind every day.

styrofoam fly
  • 578
  • 2
  • 9
  • 25
0

I think we should not be coding for you here, but I can offer some advice for the code you shared (which have too many issues).

  • You seem to have an unused variable dataSetSizes in your main function (and yet it is being printed)
  • If you are using malloc, you should be freeing such allocated memory. see free() function.
  • You may want to eventually use %f instead of %d since you will be using float values.
  • Why are you defining a function with three parameters if you are going to use only two?
  • etc...

It is a good practice to avoid writing any code until you have your logic straight. A flowchart, sequence diagrams or any UML documents may be helpful. The idea is to invest time thinking on the most efficient way to get your goal, so then, coding can be the easy part.

Alexis
  • 101
  • 3