Home > Back-end >  C code problem
C code problem

Time:04-17

Dry: enter the number and the number average and

With this problem is to practice under the custom function pointer array but how are infinite loop
Don't know where is the logical error feel as though still lack basic knowledge

 # include 
using namespace std;

//declare custom function
Int * initNumGroup (int * p_num);
Int * setNumGroup (int * p_num);
Int calNumAvg (int * p_num);

//define global variable I
Extern int I=1;

//the main function
Int main () {
Int numGroup [200].
Int * p_num=numGroup;
Cout & lt; <"Initializing an array... "& lt; P_num=initNumGroup (p_num);
Cout & lt; <"Please enter the number... "& lt; P_num=setNumGroup (p_num);
Int sum=calNumAvg (p_num);
Double avg=sum/I;
Cout & lt; <"The mean:" & lt; return 0;
}

Use the average value of the//
Int calNumAvg (int * p_num) {
int sum=0;
For (int j=1; J & lt;=I - 1; J++)
The sum +=* (p_num + j);
return sum;
}
//initialize array
Int * initNumGroup (int * p_num) {
For (int I=1; I & lt;=50; I++)
* (p_num + I)=1;
Return p_num;
}

//the user input of Numbers stored in the array
Int * setNumGroup (int * p_num) {
While (scanf_s (" % d ", (p_num + I)) & amp; & * (p_num + I)!=1)
i++;
Return p_num;

CodePudding user response:

Watched it again, error is as follows:
1. InitNumGroup function and setNumGroup function return value is meaningless, and look at the meaning of the pointer;
Definition 2. I don't need extern, extern is an external variable, not a global variable;
Starting from 0, 3) array subscripts for loop initial value is 0, the global variable I also should be 0. And, here I and global variables you repeat, although grammatically allowed, but don't do that, you want to create a new variable or a global variable? According to your logic, should be to create a new, changed to
 
Void initNumGroup (int * p_num) {
for (int j=0; J & lt; 50; I++)
* (p_num + j)=1;
}

4. What do you want to input the number 50 or 200? Parentheses is the number of elements in the array statement, is not the number of bytes, want to input the number 50 directly
 int numGroup [50]. 

5. Input scanf_s returns number is not input, but the number of data read, what do you really want to end? Also, C + + standard library should use the cin, don't use C language the scanf, end, if it is input - 1 should be changed to
 
Do
{
Cin> * (p_num + I);
} while (* (p_num + (i++))!=1);

6. Also pay attention to use the average value of the array subscript starting from 0,
 
Int calNumAvg (int * p_num) {
int sum=0;
for (int j=0; J & lt; I - 1; J++)
The sum +=* (p_num + j);
return sum;
}

7. To use the average value of a number of forced to double, otherwise the result is an integer, and be divided by the I - 1,

The complete code:
 # include 
using namespace std;

//declare custom function
Void initNumGroup (int * p_num);
Void setNumGroup (int * p_num);
Int calNumAvg (int * p_num);

//define global variable I
int i=0;

//the main function
Int main () {
Int numGroup [50].
Int * p_num=numGroup;
Cout & lt; <"Initializing an array... "& lt; InitNumGroup (p_num);
Cout & lt; <"Please enter the number... "& lt; SetNumGroup (p_num);
Int sum=calNumAvg (p_num);
Double avg=sum (double)/(I - 1);
Cout & lt; <"The mean:" & lt; return 0;
}

Use the average value of the//
Int calNumAvg (int * p_num) {
int sum=0;
for (int j=0; J & lt; I - 1; J++)
The sum +=* (p_num + j);
return sum;
}
//initialize array
Void initNumGroup (int * p_num) {
for (int j=0; J & lt; 50; J++)
* (p_num + j)=1;
}

//the user input of Numbers stored in the array
Void setNumGroup (int * p_num) {
Do
{
Cin> * (p_num + I);
} while (* (p_num + (i++))!=1);
}

  • Related