2014-09-19 31 views
-3

變爲零這是我使用的任務的一部分的碼片(這個問題的其餘部分下方)INT在陣列不斷用C

每當輸入INT到一個數組並直接從printf的它數組,它會給出正確的值。當我嘗試在函數中使用該數字時,它會給出一個零。 我使用了幾個printf語句來追蹤問題。

Enter number of Instruction classes: 
3 
Enter the frequency of the machine (MHz) 
200 
Enter CPI: 
2 
Enter Count: 
3 

a : 0 
a1 : 2 
b : 0 
b1 : 3 
z: 0 
Cycletotal: 0 
Instrtotal: 0 

有關如何解決此問題的任何想法?

void enter_params() { 
    /*declare local vars*/ 
    int n; 
    int i; 
    int f; 
    int z, a, b; 
    /*prompt for number of inst classes*/ 
    printf("Enter number of Instruction classes: \n"); 
    scanf("%d", &n); 

    /*prompt for frequency of the machine*/ 
    printf("Enter the frequency of the machine (MHz) \n"); 
    scanf("%d", &f); 
    global_n = n; 
    global_f = f; 
    /*allocate space for cpi and count arrays*/ 
    cpi = (int *)malloc(n*sizeof(int)); 
    count_i = (int *)malloc(n*sizeof(int)); 

    /*for each instruction class: */ 

    cycle_total = 0; 
    instr_total = 0; 
    for (i=0; i<n; i++) 
    { 
    /*prompt for CPI, Class, and INstruction count with prompts*/ 
     printf("Enter CPI: \n"); 
     scanf("%d", &cpi[i]); 

     printf("Enter Count: \n"); 
     scanf("%d", &count_i[i]); 

     a = cpi[i]; 
     b = count_i[i]; 

     printf("\na : " "%i", a); 
     printf("\na1 : " "%i", cpi[i]); 
     printf("\nb : " "%i", b); 
     printf("\nb1 : " "%i", count_i[i]); 

     z = cpi[i] * 3; 

     cycle_total += z; 
     printf("z: " "%i", z); 
     printf("Cycletotal: " "%i", cycle_total); 
     instr_total += b; 
     printf("Instrtotal: " "%i", instr_total); 
    } 
    return; 
} 

整個代碼:

#include <stdio.h> 
#include <stdlib.h> 
    #define NULL 0 

/*declare global vars*/ 
/*create dynamic array*/ 
/* Main MUST BE last, C compiles top to bottom, called programs must be at top, calling at bottom */ 
double *cpi=NULL, *count_i=NULL; 
int cycle_total; 
int instr_total; 
int global_n, global_f; 

void print_params() 
{ 
/* for each row, print class# (i+1), CPI[i], count_i[i]*/ 
    int i; 
    int n = global_n; 
    int j = 1; 
    printf("------------------------- \n"); 
    printf("|Class" "\t" "|CPI" "\t" "|Count" "\t" "| \n"); 
    printf("------------------------- \n"); 
    for (i=0; i<n; i++) 
    { 
     printf("|" "%i", j); 
     printf("\t" "|" "%i", cpi[i]); 
     printf("\t" "|" "%i", count_i[i]); 
     printf("\t" "| \n"); 
     printf("------------------------- \n"); 
     j++; 
    } 
    return; 
} 

float calculate_average_cpi() 
{ 
    float avg_cpi,calc; /*typecast calculation piece as float, not the final number */ 

    printf("Cycletotal" "%i", cycle_total); 
    printf("InstrTotal" "%i", instr_total); 

    avg_cpi = (float)cycle_total/(float)instr_total; 
    return avg_cpi; 
} 

float calculate_cpu_time() 
{ 
    float cpu_time; 
    cpu_time = (float)cycle_total/(float)global_f; 
    return cpu_time; 
} 

float calculate_mips(float cpu_time) 
{ 
    float mips; 
    mips = (float)instr_total/cpu_time; 
    return mips; 
} 

/* 
CPI average is: 
Σ(N, i=1) CPI[i]*Ii/Σ(N, i=1) Ii 

CPU TIme is: 
Σ(N, i=1) CPI[i]*Ii/freq 

MIPS is: 
Σ(N, i=1) Ii/CPU Time 
*/ 
/* /t is an indent */ 

void print_perf() 
{ 
/*declare local vars*/ 
    float avg_cpi = calculate_average_cpi(); 
    float cpu_time = calculate_cpu_time(); 
    float mips; 
    mips = calculate_mips(cpu_time); 

    printf("------------------------- \n"); 
    printf("|Performance" "\t" "|Value" "\t" "|" "\n"); 
    printf("------------------------- \n|Average CPI" "\t" "|" "%.2f", avg_cpi); 
    printf("\t" "|" "\n------------------------- \n" "|CPU Time (ms)" "\t" "|" "%.2f", cpu_time); 
    printf("\t" "|" "\n------------------------- \n" "|MIPS" "\t" "\t" "|" "%.2f", mips); 
    printf("\t" "|" "\n------------------------- \n"); 


    /*print table with those values*/ 
} 



void enter_params() { 
    /*declare local vars*/ 
    int n; 
    int i; 
    int f; 
    int z, a, b; 
    /*prompt for number of inst classes*/ 
    printf("Enter number of Instruction classes: \n"); 
    scanf("%d", &n); 

    /*prompt for frequency of the machine*/ 
    printf("Enter the frequency of the machine (MHz) \n"); 
    scanf("%d", &f); 
    global_n = n; 
    global_f = f; 
    /*allocate space for cpi and count arrays*/ 
    cpi = (int *)malloc(n*sizeof(int)); 
    count_i = (int *)malloc(n*sizeof(int)); 

    /*for each instruction class: */ 

    cycle_total = 0; 
    instr_total = 0; 
    for (i=0; i<n; i++) 
    { 
    /*prompt for CPI, Class, and INstruction count with prompts*/ 
     printf("Enter CPI: \n"); 
     scanf("%d", &cpi[i]); 

     printf("Enter Count: \n"); 
     scanf("%d", &count_i[i]); 

     a = cpi[i]; 
     b = count_i[i]; 

     printf("\na : " "%i", a); 
     printf("\na1 : " "%i", cpi[i]); 
     printf("\nb : " "%i", b); 
     printf("\nb1 : " "%i", count_i[i]); 

     z = cpi[i] * 3; 

     cycle_total += z; 
     printf("z: " "%i", z); 
     printf("Cycletotal: " "%i", cycle_total); 
     instr_total += b; 
     printf("Instrtotal: " "%i", instr_total); 
    } 
    return; 
} 

void quit_program() 
{ 
    if (cpi!=NULL) free(cpi); 
    if (count_i!=NULL) free(count_i); 
    return; 
} 


int main() 
{ 
    int choice = 0; 
    printf("Performance Assessment: \n"); 
    printf("1) Enter Parameters \n"); 
    printf("2) Print Table of Parameters \n"); 
    printf("3) Print Table of Performance \n"); 
    printf("4) Quit \n"); 
    scanf("%d", &choice); 
    while (choice != 4) 
    { 
     if (choice == 1) 
     { 
     enter_params(); 
     } 
     if (choice == 2) 
     { 
     print_params(); 
     } 
     if (choice == 3) 
     { 
     print_perf(); 
     } 
     printf("Performance Assessment: \n"); 
     printf("1) Enter Parameters \n"); 
     printf("2) Print Table of Parameters \n"); 
     printf("3) Print Table of Performance \n"); 
     printf("4) Quit \n"); 
     scanf("%d", &choice); 
    } 
    quit_program(); 
} 
+1

你在哪裏嘗試並在函數中使用它? – 2014-09-19 02:55:39

+0

用完整的代碼更新了我的文章。它應該是計算計算機性能的簡單任務。 – someonewhodied 2014-09-19 02:58:11

+0

爲什麼你將cpi和count_i聲明爲double的指針,但是爲它們指定了一個指向整數的指針? – Fred 2014-09-19 03:13:58

回答

0

問題:在代碼中的問題是在線路a = cpi[i]; b = count_i[i];
這裏cpi & cpi_idouble這意味着它們佔據8個字節(編譯器相關的);而a & binteger s,其大小爲4字節(依賴於編譯器)。
所以編譯器在默認情況下,使cpi & count_i指針作爲integer指針。
現在如果你看到陣列大小爲4的double array cpi = {02 xx yy zz}但相同的數組時爲大小8的int array cpi = { 0 2 x x y y z z}尋址因此cpi[0] = 0; cpi[1] = 2;

解決方案:或者聲明cpi & count_i爲整數指針(強烈推薦)或

a = int((double) cpi[i]); //Not so recomended 

再小心,而分配一個int到CPI:cpi[i] = (double) a;

作爲使用