Advertisement
uopspop

Untitled

Dec 28th, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(){
  5.  
  6. /*************** practice ******************/
  7.     // declare variables
  8.     int n;
  9.     int* scores; // 將此指標當作array來用
  10.  
  11.     // good habit - remove the remaining '\n' after using scanf
  12.     scanf("%d",&n);
  13.     char tmp_ch[1];
  14.     gets(tmp_ch);
  15.  
  16.     // get memory
  17.     size_t scores_size = sizeof(int)*n;
  18.     scores = (int*)malloc(scores_size);
  19.  
  20.     // get input and sum
  21.     int i, sum = 0;
  22.     for (i = 0; i < n; i++){
  23. //        scanf("%d",&scores[i]); // kind of redundant
  24.         scanf("%d",scores+i); // better
  25.         sum += *(scores+i);
  26.     }
  27.  
  28.     // show result
  29.     printf("sum: %d\n", sum);
  30.  
  31.     return 0; // 0 - normal exit
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement