数组中的int在C中保持为零

时间:2014-09-19 02:53:03

标签: c arrays

这是我用于部分作业的代码片段(在本问题的其余部分下面)

每当我将int输入数组并直接从数组中打印出来时,它就会给出正确的值。当我尝试在函数中使用该数字时,它给出零。 我使用了一些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 个答案:

答案 0 :(得分:0)

问题:代码中的问题在行a = cpi[i]; b = count_i[i];中 这里cpi&amp; cpi_idouble,这意味着它们占用8字节(依赖于编译器);而a&amp; binteger,大小为4个字节(取决于编译器) 所以编译器默认生成cpi&amp; count_i指针integer指针 现在,如果您将数组视为大小为double array cpi = {02 xx yy zz}且大小为int array cpi = { 0 2 x x y y z z}时为cpi[0] = 0; cpi[1] = 2;

解决方案:要么声明cpi&amp; count_i作为整数指针(强烈推荐)或

用作:

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

在为cpi指定int时再次小心:cpi[i] = (double) a;