Array of floating point numbers using a malloc function.

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 5799 Views - Last Post: 09 March 2012 - 09:42 PM Rate Topic: -----

#1 blu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 01-February 12

Array of floating point numbers using a malloc function.

Posted 08 March 2012 - 04:14 PM

Hello again. As usual I've arrived at another road block on my trek in the world of C language.
As explained in the title I am to write a program that prints an array of floating point numbers using a malloc function.
Well, I did that successfully, to a point.
At first I planned to just print the array as integers. Which I did as you can see by the code I'm about to post. Now that I want to change the integers to floats It doesn't seem to be accepting.
F:\cet\malloc.c||In function 'main':|
F:\cet\malloc.c|11|error: invalid operands to binary + (have 'float *' and 'float')|
F:\cet\malloc.c|14|error: invalid operands to binary + (have 'float *' and 'float')|
||=== Build finished: 2 errors, 0 warnings ===|
There are my errors as compiled with floating point numbers.

I would also like to add in I am instructed to calculate the average of numbers entered. You will see how I attempted this, with the code commented out. I will attach a photo of my compiler of my attempt to calculate the average of my array. As you can see in the jpeg the average is incorrect. I'm not sure if that's a memory leak or what. . .
Here is my code;
int main(void)
{
    int *list, sortnum, j, average = 0, sum = 0;
    printf("Enter the number of data points to average: ");
    scanf("%d",&sortnum);
    list = (int *)malloc(sortnum * sizeof(int));
    for (j = 0; j < sortnum; j++)
        scanf("%d", list + j);
    printf("You entered the following:\n");
    for (j = 0; j < sortnum; j++)
        printf("%d\n", *(list + j));
    free(list);

    /*sum = *(list + j) / sortnum + list;
    average = list + j / sortnum;
    printf("%d", average);*/


    return 0;

}




I hope I've given you all of the information you need to give me a helping hand.
Any feedback will be appreciated, thanks.

blu

This post has been edited by blu: 08 March 2012 - 04:17 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Array of floating point numbers using a malloc function.

#2 shurd  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 162
  • Joined: 05-February 12

Re: Array of floating point numbers using a malloc function.

Posted 08 March 2012 - 04:33 PM

Could you post your code with the float try?

For you average question your sum does not have all of the numbers in the list, you need to go through all your list, as you did for printing it. And you should free it after you do the sum.

This post has been edited by shurd: 08 March 2012 - 04:36 PM

Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

Re: Array of floating point numbers using a malloc function.

Posted 08 March 2012 - 04:35 PM

What picture?

list = (int *)malloc(sortnum * sizeof(int));


You're writing C. There's no need for the (int *) cast there, so get rid of it. If you need it to shut the compiler up, then you're either compiling as C or you've forgotten to #include <stdlib.h>.
Was This Post Helpful? 1
  • +
  • -

#4 blu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 01-February 12

Re: Array of floating point numbers using a malloc function.

Posted 08 March 2012 - 04:50 PM

Sure, I can post a snippet of my attempt at floating point numbers;
    float *list, sortnum, j, average = 0, sum = 0;
    printf("Enter the number of data points to average: ");
    scanf("%f",&sortnum);
    list = (float *)malloc(sortnum * sizeof(int));
    for (j = 0; j < sortnum; j++)
        scanf("%f", list + j);
    printf("You entered the following:\n");
    for (j = 0; j < sortnum; j++)
        printf("%f\n", *(list + j));
    free(list);




I'm not entirely sure if I understand what you mean about my average calculation. Ill read it again after I make this post.

View PostJackOfAllTrades, on 08 March 2012 - 04:35 PM, said:

What picture?

list = (int *)malloc(sortnum * sizeof(int));


You're writing C. There's no need for the (int *) cast there, so get rid of it. If you need it to shut the compiler up, then you're either compiling as C or you've forgotten to #include <stdlib.h>.


I'm sorry. I guess I carelessly forgot to attach it.
Hopefully it worked this time. As you can see, that average calculation is incorrect.

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

Re: Array of floating point numbers using a malloc function.

Posted 08 March 2012 - 05:01 PM

You cannot use a float as an index to an array...i and j CANNOT be floats.
Was This Post Helpful? 0
  • +
  • -

#6 shurd  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 162
  • Joined: 05-February 12

Re: Array of floating point numbers using a malloc function.

Posted 08 March 2012 - 05:04 PM

float *list, sortnum, j, average = 0, sum = 0;



You don't need every variable to be float

float *list,average = 0,sum =0;



This you need, the others will store int values as sortnum marks the amount of numbers you want in the array and you can't have half a number in it, and j goes through the indexes of the list, and you cant go through half an index as you would be acessing memory of 2 half indexes.

For the average you need your sum to hold the value of all numbers in the list, than divide it by the total which is sortnum in your case.

This post has been edited by shurd: 08 March 2012 - 05:15 PM

Was This Post Helpful? 1
  • +
  • -

#7 blu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 01-February 12

Re: Array of floating point numbers using a malloc function.

Posted 08 March 2012 - 06:10 PM

Thank you shurd. Floating point numbers are now successfully being printed.
I moved on, trying to calculate the average for the array. Here's what I tried.
sum  = *list + sortnum;
    average = sum/sortnum;
    printf("Average %d", average);


What I thought that would do was assign the value "sum" by adding all of the entered numbers together, then "average" would be assigned to the calculation of the sum divided by the amount of numbers entered.
Regardless of what I enter the average always equals 0.
Was This Post Helpful? 0
  • +
  • -

#8 shurd  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 162
  • Joined: 05-February 12

Re: Array of floating point numbers using a malloc function.

Posted 08 March 2012 - 06:28 PM

Nope, you do as you did to print using a loop that goes through all the memory of the list you need.
Than for each loop you add the sum to it last value plus the value of the new number stored in the memory.
Was This Post Helpful? 0
  • +
  • -

#9 blu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 01-February 12

Re: Array of floating point numbers using a malloc function.

Posted 08 March 2012 - 08:33 PM

View Postshurd, on 08 March 2012 - 06:28 PM, said:

Nope, you do as you did to print using a loop that goes through all the memory of the list you need.
Than for each loop you add the sum to it last value plus the value of the new number stored in the memory.


Wouldn't I have to create an array integer?
Something along the lines of this?
int array[10] = {1,2,3,4,5,6,7,8,9,10};
for (int i = 0; i < 10; ++i)
sum+=array[i];
average = sum/10;



I tried to do that. I just dont know how to assign an array integer as depicted in this snipped with my list.
Was This Post Helpful? 0
  • +
  • -

#10 shurd  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 162
  • Joined: 05-February 12

Re: Array of floating point numbers using a malloc function.

Posted 08 March 2012 - 08:39 PM

@blu - That is exactly what you will do but with an dynamic array

Take a look at your printing way:
for (j = 0; j < sortnum; j++)
    printf("%f\n", *(list + j));


That is the same as doing
for (j = 0; j < sortnum; j++)
    printf("%f\n", list[j]);



So you can do the same code you posted above for the float average
for (j = 0; j < sortnum; j++)
    //sum being added here in one of the two ways above


Was This Post Helpful? 0
  • +
  • -

#11 blu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 01-February 12

Re: Array of floating point numbers using a malloc function.

Posted 08 March 2012 - 09:24 PM

Forgive me for not getting my mind around this, but I'm still having trouble.
I added a for loop for my average calculation. Here is what I have now.
     for (j = 0; j < sortnum; j++)
{
    sum += j;
	average = sum/sortnum;
	printf("%d", average);
}


I just don't understand as to why I'm not getting any results at all. It's just 0

This post has been edited by blu: 08 March 2012 - 09:26 PM

Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

Re: Array of floating point numbers using a malloc function.

Posted 09 March 2012 - 04:24 AM

average is a float, and therefore requires a "%f" specifier, not "%d".

Next up, I want you to THINK! What are you summing up here?

for (j = 0; j < sortnum; j++)
{
    sum += j;
    average = sum/sortnum;
    printf("%d", average);
}



Does it make sense mathematically to be calculating the average in a loop?
Was This Post Helpful? 1
  • +
  • -

#13 blu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 01-February 12

Re: Array of floating point numbers using a malloc function.

Posted 09 March 2012 - 10:10 AM

Making some more progress.
I can calculate the average of whole numbers, however, the average of decimal point numbers isn't consistent with the calculator I'm using to compare.
Here is how I modified my code;
for (j = 0; j < sortnum; j++)
{
    sum += *list;

}
    average = sum / sortnum;
    printf("The average of your entered numbers: %.2f", average);



In my previous code, I had
sum += j;
To my understanding "j" is a placeholder for the numbers entered in "*list", which in the numbers entered by the user. Our professor gives us a base code to work off of for our assignments. I'm having trouble understanding the given code, but I believe I'm getting some sort of understanding of it now.
Now If I'm correct
sum += *list
adds the specific numbers entered (list) and stores it into the variable sum; and
average = sum / sortnum;
takes the total numbers added together from list and divides it by how many numbers were entered (sortnum).
I hope you can tell me if I'm getting an understanding of how this works. I basically got to where I am in this code through random trial and error, and the help from you guys, of course.
Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: Array of floating point numbers using a malloc function.

Posted 09 March 2012 - 10:23 AM

Show your current code, a sample of your input, what you expect the output to be and what the output actually is.

Jim
Was This Post Helpful? 0
  • +
  • -

#15 shurd  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 162
  • Joined: 05-February 12

Re: Array of floating point numbers using a malloc function.

Posted 09 March 2012 - 10:59 AM

 sum += *list;



You are going through only the first index, you need to go through all. Try adding j to that
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2