2 Replies - 722 Views - Last Post: 11 July 2011 - 03:04 PM Rate Topic: -----

#1 wickedwhitetails  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-June 11

Define and use a macro sumarray to sum the values of a numeric array.

Posted 11 July 2011 - 02:42 PM

Ok well I have been working on this assignment and I think that I am very close but it just doesnt seem to finish. It compiles and flashes up the little black window after compiling but it doesnt keep it open so I dont know if it is doing what it need to do or not. If anyone could help me to get to the window to stay open so that I can see if this is working that would be great.

#include <stdio.h>
#define SUMARRAY(arr) \
	sum = 0; \
for(int i = 0; i < (sizeof(arr) / sizeof(arr[0])); i++) \
{ \
sum += arr[i]; \
}
int main(void)
 {
	 int sum;
  int arr1[] = {1, 2};
  int arr2[] = {1, 2, 3};
  int arr3[] = {1, 2, 3, 4};
  int arr4[] = {1, 2, 3, 4, 5};
  int arr5[] = {1, 2, 3, 4, 5, 6};
  SUMARRAY(arr1);
  printf("Sum of the values in arr1 is: %d\n", sum);
  SUMARRAY(arr2);
  printf("Sum of the values in arr2 is: %d\n", sum);
  SUMARRAY(arr3);
  printf("Sum of the values in arr3 is: %d\n", sum);
  SUMARRAY(arr4);
  printf("Sum of the values in arr4 is: %d\n", sum);
  SUMARRAY(arr5);
  printf("Sum of the values in arr5 is: %d\n", sum);
  return 0;
 } 



Is This A Good Question/Topic? 0
  • +

Replies To: Define and use a macro sumarray to sum the values of a numeric array.

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1398
  • View blog
  • Posts: 4,873
  • Joined: 19-February 09

Re: Define and use a macro sumarray to sum the values of a numeric array.

Posted 11 July 2011 - 03:02 PM

You can hold the console window open, by reading a character at the end of the program.


  getchar();

  return 0;
}


Was This Post Helpful? 0
  • +
  • -

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: Define and use a macro sumarray to sum the values of a numeric array.

Posted 11 July 2011 - 03:04 PM

You need to pause your program's execution before return 0;. I believe you can use getchar() before return 0 to pause the program execution for character input ; however, you may need to flush the input buffer first.


Edit: ninja'd

This post has been edited by ImaSexy: 11 July 2011 - 03:06 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1