arrays and functions

calculating days between two months

Page 1 of 1

2 Replies - 663 Views - Last Post: 30 November 2010 - 08:08 PM Rate Topic: -----

#1 defunktlemon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 29-November 10

arrays and functions

Posted 29 November 2010 - 08:02 PM

Hi:
I'm real new to programming and have the following program which is an exercise in using functions and arrays but is not quite working. Could somebody help me understand what is wrong with it and why please?
The program should take an input from the user of two dates, day/month/2010, and return a value of the number of days between them.

Program:

/*Program to read user input of two dates and return the number of days between them*/
#include <stdio.h>

int month_days[12] = {31, 29, 30, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //declare array and assign number of variables...

int monthdays(int m1, int m2)//formal parameters...
{
	int numDays=0;
	//...compute correct numDays here
	for(i=m1-1; i<=m2-1; i++)
		numDays = numDays + m1[i];
	
	return numDays;
}

int main()
{
	int days, month_1 = 0, month_2 = 0, months_between = 0; //declare integer variable...

	int numOfDays;

	printf("Please input a number between 1 and 12 for the month: \n");
	scanf("%d", &month_1);
	printf("Now input a number between 1 and 12 for the second month: \n");
	scanf("%d", &month_2);
	
	months_between = month_2 - month_1;
	printf("The number of months between them are: %d\n", months_between);

	numOfDays = monthdays(month_1, month_2);//actual parameters...
	printf("The number of days between them are: %d\n", numOfDays);

	return 0;
}



Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: arrays and functions

#2 Maxwellius  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-November 10

Re: arrays and functions

Posted 29 November 2010 - 08:27 PM

In the function monthdays, you declare that m1 and m2 are integers, yet you use m1 as an integer array.
Was This Post Helpful? 0
  • +
  • -

#3 defunktlemon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 29-November 10

Re: arrays and functions

Posted 30 November 2010 - 08:08 PM

Hi Maxwellius:
Thanks for your reply.
Do you mean the line:

numDays = numDays + m1[i];

has the error? Specifically, m1[i], is using m1 as an integer array?

regards
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1