2013-03-23 42 views
1

我正在努力與這個程序的最後一點,我需要將數組傳遞給兩個不同的功能,我不知道該怎麼做。如何將數組(1D)傳遞給使用C中指針的函數?

唯一的錯誤,我得到發生: 這裏:

input(array[20]); 
calculate(array[20],&pairs); 

這裏:

//exit, 
exit; 

除此之外,它應該工作,我需要它的樣子,我想通了如何使用指針正常變量,但陣列行爲不同,我不知道該怎麼辦...

該文件是一半完成,我仍然必須添加循環在e nd說明概述,但我只需要幫助傳遞數組。

此外,涉及退出行的錯誤與問題無關,但如果您知道某個修復功能太棒了!

/* 
Description: Do not use global variables. Pass your arguments by value and 
      by reference. Using arrays and modular programming techniques, 
      write a C program that will allow a user to populate an array 
      with integers, and then compute and print out the number of 
      adjacent pairs in the array (i.e. the number of occurrences where 
      an array element is the same as its neighbors). For example, if 
      the array contained [2,3,3,4,52,52,4,4,4,4,7,7,1], the number of 
      adjacent pairs is 6. The program should give the user the option 
      of examining more than one array (i.e. loop). Assume the array has 
      a max. of 20 elements. The main() function should primarily be 
      responsible to direct the flow of logic. That is: use one 
      function to obtain input (pass by reference), another function to 
      do processing (pass by value), and perhaps a third function to do 
      output (pass by value). The main() function should call the input 
      function and the processing function. 
*/ 

//Include statements. 
#include <cstdlib> 
#include <iostream> 
#include <math.h> 

//Standard namespace. 
using namespace std; 

void input (int array[20]); //Used when user inputs the numbers. 
void calculate(int array[20], int *pairs); //Used to calculate the matches. 
void output(int *pairs); //Used to output the number of pairs. 

int main(void) 
{ 
    int array[20] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; 
    char quit; 

    start: 
    int pairs = 0; 
    input(array[20]); 
    calculate(array[20],&pairs); 
    output(&pairs); 

    //Ask the user if they want to exit 
    printf("\nWould you like to continue testing my project, or exit?"); 
    printf("\nTo exit input: 'N' or 'n'. To continue testing input anything else."); 
    //store their input in variable: exit 
    scanf("%s",&quit); 
    //If they want to exit... 
    if (quit == 'N' || quit == 'n') 
    { 
     //exit, 
     exit; 
    } 
    //otherwise, 
    else 
    { 
     //clear the screen 
     system("cls"); 
     //and go back to the start. 
     goto start; 
    } 
} 

void input(int array[20]) 
{ 
    int count = 0; 
    for (count;count<20;count++) 
    { 
     printf("Enter values . . . \n"); 
     scanf("%i", &array[count]); 
    } 
} 

void calculate(int array[20], int *pairs) 
{ 
    int counter = 0; 
    for (counter;counter<19;counter++) 
    { 
     if (array[counter] == array[counter+1]) 
      *pairs+=1; 
    } 
} 

void output(int *pairs) 
{ 
    printf("Number of pairs: [%i]\n", *pairs); 
} 
+0

'退出;'應該是'return 0;' – 2014-04-07 23:24:34

回答

0

當您調用函數時,您不傳入大小。

input(array); 

這應該夠了。


另一種解決方案是使功能取int*。你會添加一個額外的參數來傳遞數組的大小。

void func(int* my_array, int size); 

您將宣佈你的陣列,像這樣:

int i[20]; 

並調用你的函數是這樣的:你要返回

func(i, 20); 

目前,通過傳遞array[20]一個int,你會出界,以防萬一你想知道,因爲最大索引是19。表達式的不適當的返回類型不允許您編譯程序。

+0

當傳遞陣列工作時取出大小,謝謝! – Wafer 2013-03-23 23:56:19

+0

int * my_array'和'int my_array [20]'在函數參數列表中完全相同,我不明白「你正在返回一個int」的意思。「 – 2014-04-07 23:24:14

+0

@MattMcNabb我只是像你一樣迷失。我在一年前寫了這個答案。 – 2014-04-08 17:50:42