Reading floats into an array

后端 未结 6 794
[愿得一人]
[愿得一人] 2021-01-22 00:39

How could I read let\'s say 10 floats and store them in an array without wasting any memory?

相关标签:
6条回答
  • 2021-01-22 01:15

    Aha. It's not reading the floats that's the problem, it's the memory. You read in i, and you need an array that holds exactly i floats.

    This really does smell like homework, which is fine, but I'm too much the teacher to give you the full answer. So I'll tell you, what you need is a C function named malloc() and a C operator (it looks like a function but it's actually built into the language) named sizeof.

    Have a look at this tutorial.


    Yup, you got it there. Here's the code from your comment, formatted.

    int n,index;
    float temp;
    scanf("%d",&n);
    float *pValues=(float *)calloc(n,sizeof(float));
    for(index=0;index<n;index++) {
        scanf("%f",&temp); 
        *(pValues+index)=temp;
    }
    

    I'd do it with two changes:

    1. Its more idiomatic to use malloc for anything besides characters
    2. In C, arrays and pointers have a very close relationship; in fact *(pValues+index) is exactly equivalent to pValues[index].

    So I'd rewrite this as:

    int n,index;
    float temp;
    scanf("%d",&n);
    float *pValues=(float *)malloc(n*sizeof(float));
    for(index=0;index<n;index++) {
        scanf("%f",&temp); 
        pValues[index]=temp;
    }
    

    Let's look at one more transformation of the code. You have pValues, which is a pointer to float. You have &temp, which is also a pointer to float, because & is the address-of operator and temp is a float. AND, you're just doing pointer arithmetic with your index. So, we could rewrite this one more time as:

    int n,index;    // Don't need temp
    scanf("%d",&n);
    float *pValues=(float *)malloc(n*sizeof(float));
    for(index=0;index<n;index++) {
        scanf("%f",pValues+index); 
    }
    

    Now, quiz question: what would happen if you made the loop

    for(index=0;index<n;index++) {
        scanf("%f",pValues++); 
    }
    
    0 讨论(0)
  • 2021-01-22 01:21

    You'll have to be more specific about the problem.

    Unless you have something else that you need to do with these numbers, then the easiest way to save memory is not to store them in an array to begin with.

    It sounds like what you want is something like.

    sum = 0;
    do
      read current
      sum += current
    while (you haven't yet read 10 numbers);

    0 讨论(0)
  • 2021-01-22 01:24
    float arr[10];
    for(i = 0; i < 10; i++){
        scanf("%f", &arr[i]);
    }
    

    I know, it's like the above, but doesn't use the extra int in case it's not optimized out. hehe

    To use even less memory us the code below. They're all read into an array, but only the last one to be read in is in the array:

    float arr[1];
    for(i = 0; i < 10; i++){
        scanf("%f", &arr[0]);
    }
    
    0 讨论(0)
  • 2021-01-22 01:30

    sounds like homework.

    read the number of floats from the input. multiply that by the size of an individual float allocate that exact number of bytes storing the address in a pointer to floats have a loop read in the floats from the input have a loop that adds up all the floats in the array

    My guess here is the homework lesson is to realize the connection between the term pointer and array, although you can do this without any array indexing, but your question implies that you have to use an array.

    0 讨论(0)
  • 2021-01-22 01:33
    int size = 10;
    float vet[size];
       for(i = 0; i < size; i++){
          scanf("%f", &vet[i]);
       }
    

    As simple as it could be :)

    0 讨论(0)
  • 2021-01-22 01:37

    The answer to your question is that you cannot add things to an array and expect the array to use any memory.

    0 讨论(0)
提交回复
热议问题