2015-02-10 77 views
0

我调用函数从用户获取数组并操作该数组。我的问题是我试图从用户那里得到10个整数。输入10个整数后,另一个随机整数(可能是内存位置?)会自动添加到我的数组的末尾。正如你所看到的2686672已被众神添加。我不确定我的问题在哪里。数组返回一个额外的值

我的输出实际上列在代码的顶部。我试图张贴输出的照片,但我是一个新手,所以我不能这样做。

The values of the array as entered are: 1 2 3 4 5 6 7 8 9 10 2686672 
The values of the sorted array are: 1 2 3 4 5 6 7 8 9 10 2686672 
The largest number in the set is 2686672 
The smallest number in the set is 1 
The average of the number set is 244247.906250 
The standard deviation of the number set is 244242.406250 
10 

代码:

// include libraries 
#include <stdio.h> 
#include <stdlib.h> 
#include <math.h> 

// declare functions 
int max (int count, int array[]); 
int min (int count, int array[]); 
int getArray (int array[]); 
float stDev (int count, int array[]); 
float avg (int count, int array[]); 
void printArray (int count, int array[]); 
void sortArray (int count, int array[]); 


int main() 
{ 
    // declare variables and arrays 
    int count = 10; 
    int array[count]; 

    // Give user instructions 
    printf("Please enter an integer followed by the enter key.\n"); 
    printf("User input will halt after ten integers, \nor if a number less than zero is entered.\n"); 
    count = getArray(array);    // call function to get user input 
    printf("The values of the array as entered are: "); 
    printArray (count, array);    // call function to print array as it was entered 
    sortArray (count, array);    // call function to sort the array 
    printf("The values of the sorted array are: "); 
    printArray (count, array);    // call function to print the sorted array 
    printf("The largest number in the set is %d\n", max (count, array));  // call function and print the max value 
    printf("The smallest number in the set is %d\n", min (count, array));  // call function and print the min value 
    printf("The average of the number set is %f\n", avg(count, array));   // call function and print the average value 
    printf("The standard deviation of the number set is %f\n", stDev (count, array));  // call function and print the standard deviation 
    printf("%d",count); 

    return 0; 
} 
//****************** FUNCTIONS ************************* 


/* 
Function name: getArray 
Inputs: The address array[0] 
Outputs: Integers entered for the array. Up to 10 integers 
Description: This function gets user values for the array for up to 10 separate integers.\ 
      If a negative number, that will be the last input allowed. 
*/ 

int getArray(int array[]){ 
    int N=10, c;   // declare local variables 
    // for loop that places the integer input into the array 
    for(c=0; c<N; c++){ 
     scanf("%d", &array[c]); // place input into array location for that particular loop 
     // if a negative number is entered, force for loop end 
     if (array[c]<0) 
      N=c; 

    } 
    return c;  // return the # of integers entered 
} 


/* 
Function name: avg 
Inputs: pointers for array and the number if integers in the array 
Outputs: The average value of the array contents 
Description: This function serves to take the individual integers in the array, add them 
      all together, and divide by the number of integers i.e. find the mean value 
      of the array. 
*/ 

float avg(int count, int array[]){ 
    int c;  // set counter to integer 
    float temp = 0.0; // set temp to float 
    // for loop that adds all the integers in the array 
    for (c=0; c<= count; c++){ 
     temp = temp + array[c];  // add current value of the array to temp 
    } 
    temp = (temp/c);    // divide added integers by the number of integers 
    return temp;     // return the mean value of the array 
} 


/* 
Function name: max 
Inputs: pointers for array and the number if integers in the array 
Output: the max value of the integers in the array 
Description: This function serves to find the maximum value of the array 
      by comparing every integer to the current maximum. 
*/ 
int max (int count, int array[]){ 
    int temp = array[0], c;  // declare variables and set the first integer of the array to max 
    // for loop to compare current max to current integer 
    for (c=1; c<= count; c++){ 
     // if the integer at the current array address is larger than the current max, replace it 
     if (array[c] > temp) 
      temp = array[c]; 
    } 
    return temp;  // return max value of the array 
} 

/* 
Function name: min 
Inputs: pointers for array and the number if integers in the array 
Output: the min value of the integers in the array 
Description: This function serves to find the minimum value of the array 
      by comparing every integer to the current minimum. 
*/ 
int min (int count, int array[]){ 
    int temp = array[0], c;  // set the first integer in the array to minimum 
    // for loop that compares current integer to current min 
    for (c=1; c<= count; c++){ 
     //if integer at current array address is less than the current min, replace it 
     if (array[c] < temp) 
      temp = array[c]; 
    } 
    return temp;  // return the minimum integer value of the array 
} 

/* 
Function name: stDev 
Inputs: pointers for array and the number if integers in the array 
Output: the standard deviation of the integers in the array 
Description: This function serves to find the standard deviation of the array 
      by comparing every integer to the mean value. The differences are 
      squared and added together. Then the square root of the total is taken 
      which is the standard deviation 
*/ 
float stDev (int count, int array[]){ 
    float temp = 0.0, average;   // declare variables as float 
    int c;      // declare counter as int 
    average = avg(count, array); // call function to get average value of the array integers 
    // for loop that compares the values of every integer to the mean, squares them, and adds them together 
    for (c=0; c< count; c++){ 
     temp = pow(average - array[c], 2) + temp; 
    } 
    temp = sqrt(temp/c);  // take the square root of the total to find standard deviation 

    return temp;    // return the standard deviation of the array integers 
} 

/* 
Function name: printArray 
Inputs: pointers for array and the number if integers in the array 
Output: prints the array 
Description: This function serves to print the integer values of the array 
*/ 
void printArray (int count, int array[]){ 
    int c;  // set counter to integer 
    // for loop that prints the integer at the current array address prescribed by the for loop 
    for (c=0; c<= count; c++){ 
     printf("%d ", array[c]);  // print the individual integers 
    } 
    printf("\n");      // new line 
} 

/* 
Function name: sortArray 
Inputs: pointers for array and the number if integers in the array 
Output: the integers in the array in order from lowest to highest 
Description: This function serves to put the values of the array in 
      order from lowest to highest 
*/ 
void sortArray (int count, int array[]){ 
    int temp, c, d, cc;  // declare variables and counters to integer 
    // primary for loop that makes the secondary loop run as many times as the array has values 
    for (c=0; c<= count; c++){ 
     // for loop that compares side by side values for increasing values, if they are not increasing, swap them 
     for (cc=0; cc< count; cc++){ 
      d= cc+1;  // set d to be one memory location higher than the the current counter value 
      // if the integers are not in increasing order, swap them 
      if (array[cc] > array[d]){ 
       temp = array[d]; 
       array[d] = array[cc]; 
       array[cc] = temp; 
      } 
     } 

    } 
} 

提前感谢您的帮助!

+0

为什么'getArray'不像所有其他函数一样将'count'作为参数? – Barmar 2015-02-10 02:05:19

+0

它的设置方式,因为如果输入负数,函数可以更改计数值。这对我来说似乎是合乎逻辑的,现在它将我的循环更改为“c 2015-02-10 02:14:49

回答

0

在所有for循环中,应该使用c < count作为重复测试。其中一些有c <= count,所以他们正在做一个额外的迭代,并超出阵列的末尾阅读。

+0

谢谢,这很好。我的想法是,我的投入是问题,而不是我的输出。 – 2015-02-10 02:16:37

0

C中的数组从0开始索引,这似乎是你理解的。但是,这意味着如果数组的大小为N,则有效索引为0N - 1。您正在使用从0N几乎无处不在的索引。因此额外的垃圾元素。

该循环在avg例如

for (c=0; c<= count; c++){ 
    temp = temp + array[c];  // add current value of the array to temp 
} 

从上述错误受到影响。它从0循环到count,而不是从0循环到count - 1。在这样的循环继续条件通常应该使用进行严格比较

for (c = 0; c < count; c++){ 

对于您stDev周期正确写了一些无法解释的原因,而大部分的相似周期是不正确的。

+0

并非无处不在。他在'getArray'和'stDev'中做了正确的处理。 – Barmar 2015-02-10 02:04:36