【问题标题】:C Programming Dynamic ArrayC 编程动态数组
【发布时间】:2015-05-07 23:53:29
【问题描述】:

无法理解如何正确执行此操作。我以 5 3 2 4 1 -1 的形式从用户那里获得输入,一旦输入 -1,它就会停止请求输入并将整数放入排序的数组中。然后它找到中位数。如何正确地做一个动态数组并将整数返回一个数组。

#include <stdio.h>
#include <stdlib.h>

int * readNumbers( int * total );
int computeMedian(int *p, int n);

// Please do not change main() function

int main(int argc, const char * argv[])
{

   int n ; // the number of positive integers you input,
            // it will be brought back by using call-by-reference next line.
   int * arr = readNumbers( & n );

   if( arr != NULL && n > 0 ) {

      int median = computeMedian(arr, n);
      printf("The median for the group of numbers you input is: %d \n", median );
   }
   else{

      printf("No positive number has been input! \n");
   }

   free(arr); //deallocate what arr points to.

   return 0;
}


/**
 * The function continuously reads in postive integer numbers ( or zero )
 * from the starndard input, until users input (-1).
 * The group of positive numbers(or zero) are saved into a dynamic array,
 * a piecie of memory that is dynamically allocated in this function.
 *
 * The function returns the pointer that points to the array of postive
 * numbers(or zero). The function returns NULL if no positive number(or zero) is inputted,
 * that is, you first input a -1 on stardout.
 *
 * You do not need to check whether an input is postive(or zero) or not, except for checking -1 used to
 * terminate the input. You can safely ASSUME all inputs are postive numbers ( or zero ) except for the
 * last -1 input.
 *
 *
 * Note: all memory (elements) in the returned dynamic array have to be used,
 * which means no memory in the returned dynamic array is wasted and vacant.
 * The returned array has the exact amount of spaces to hold all inputted positive numbers(or zero).
 * Note: in your program you can assume there is no more than 1000 numbers that
 * uers will input on starndard input.
 */
int * readNumbers( int * total )
{
   total = 0;
   int array[1000]; 
   int integer, i; 

   do
   {
      printf("Enter:");
      scanf("%d", &integer); 
      array[total] = integer;
      total++;
      return * integer;
   } while(integer != -1);

}




/**
 * The function takes an array of postivie numbers, pointed by p,
 * the size of the array is specified by n.
 *
 * The function first sorts the array p, then computes and returns the median number in p.
 * The median number can be calculated using the following equations AFTER p is sorted.
 * Note: you can sort the array by using any sorting algorthims you know.
 *
 * Assuming the array p is sorted and is of length n,
 * If n is odd then the median is p[(n-1)/2].
 * If n is even than the median is ( p[n/2] + p[(n/2)-1] ) / 2.
 *
 */
int computeMedian(int *p, int n)
{
}

【问题讨论】:

  • 我该怎么做呢?像 readNumbers 函数中的 void * malloc(total) 一样?
  • 你的问题没有说明你不明白什么。
  • return * integer; 您希望此声明会发生什么?
  • int *array = malloc(1000*sizeof(int)); 而不是 int array[1000];
  • 你为什么将指向n 的指针传递给函数而不是仅仅传递n 本身?为什么该函数会忽略该参数并将其设置为0?为什么要尝试使用指针作为数组索引?你似乎对这些东西的作用完全没有概念,我认为你需要回归基础。

标签: c arrays dynamic


【解决方案1】:
int *readNumbers( int *total ){
    *total = 0;
    int *array = malloc(1000 * sizeof(int));
    int integer, i; 

    while(1){
        printf("Enter:");
        if(*total == 1000 || 1 != scanf("%d", &integer) || integer == -1)// integer < 0
            break;

        array[(*total)++] = integer;
    }
    if(*total)
        return realloc(array, *total * sizeof(int));
    else {
        free(array);
        return NULL;
    }
}

【讨论】:

  • 动态数组如何?我确信 OP 希望将项目附加到可以动态增长的列表中。
  • 我忘记了必须使用返回的动态数组中的所有内存(元素)已修复。
  • 但是你知道这仍然不是一个动态数组,对吧?这只会浪费 CPU 时间,因为如果用户输入 999 个数字,realloc 将不得不将 999 个元素复制到一个较小的数组中。除此之外:如果用户在这个过程中输入了超过 1000 个数字怎么办?
  • 绝对是动态数组。 在您的程序中,您可以假设用户在标准输入中输入的数字不超过 1000 个。
猜你喜欢
  • 2016-02-22
  • 2012-07-12
  • 1970-01-01
  • 1970-01-01
  • 2020-09-13
  • 2011-10-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多