Thread: C programming Help

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    21

    Exclamation C programming Help

    Alright I need some help with my homework, it really shouldn't take much time to fix but I cannot find my problem. I did a check, and for some reason I keep getting null pointers in most of my functions, and in my convertTime function the numbers are way higher than they should be. Any help is apreciated. Here is my code

    Code:
    //Utils.H header file----------------------------------------------
    
    
    
    
    //This function takes the radius of a circle and returns
    //the diameter, the circumference and the area. 
    int circleStatistics(double radius, double *diameter, double *circumference, double *area);
    
    
    //This function takes a number of days and returns how many 
    //years, weeks and remaining days that is. 
    int convertTime(int days, int *y, int *w, int *d);
    
    
    //This function takes a length of time and calculates the 
    //dilation of that time at a percentage of the speed of light. 
    int lorentzTimeDilation(double normalTime, double percentC, double *dilatedTime);
    
    
    
    
    //This function calculates the arithmetic/geometric mean of the function.
    double agm(double x, double y, double epsilon);
    
    //Utils.C file----------------------------------------------------
    
    
    #include<stdlib.h>
    #include<stdio.h>
    #include<math.h>
    #include "utils.h"
    
    
    int circleStatistics(double radius, double *diameter, double *circumference, double *area) {
    	if (radius<0) {
    		return 1;
    	}else if (diameter==NULL || circumference==NULL || area==NULL) {
    		return 1;
    	} else {
    		*diameter=radius*2;
    		*circumference=(*diameter)*3.14159;
    		*area=radius*radius*3.14159;
    	}
    }
    
    
    
    
    int convertTime(int days, int *y, int *w, int *d) {
    	if (days<0) {
    		return 1;
    	}else if (y==NULL || w==NULL || d==NULL) {
    		return 1;
    	}else {
    		*y=days/365.0;
    		*w=(days%365)/7.0;
    		*d=days%7;
    	}
    }
    
    
    int lorentzTimeDilation(double normalTime, double percentC, double *dilatedTime\
    ) {
    	if (normalTime<0) {
    		return 1;
    	}else if (percentC>1 || percentC<=0) {
    		return 1;
    	} else if (dilatedTime==NULL) {
    		return 1;
    	}else {
    	  *dilatedTime=normalTime/sqrt(1+((percentC*percentC)));
    		return 0;
    	}
    	
    }
    
    
    
    
    double agm(double x, double y, double epsilon) {
    	double a1, a2, g1, g2; 
    	a1=.5*(x+y); 
    	g1=sqrt(x*y); 
    	while (a1-g1<epsilon) {
    		a2=.5*(a1+g1); 
    		g2=sqrt(a1*g1);
    		a1=a2;
    		g1=g2;
    	}
    	return (a1+g1)/2;
    };
    
    //UtilsTester.c---------------------------------------------------
    
    
    
    
    
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    #include "utils.h"
    
    
    //This program will test my utils functions with a number of test cases
    int main(int argc,char** argv) {
      int test;
      double *diameter;
      double *circumference;
      double *area;
      //Testing CircleStatistics
      //Test 1
      test=circleStatistics(0.0, diameter, circumference, area);
      //expected results
      printf("Expected:diameter: 0.00, circumference 0.00, area 0.00\n");
      //actual results
      printf("Actual:diameter: %.2f, circumference %.2f, area %.2f\n", diameter, circumference,area);
      //Test 2
      test=circleStatistics(1, diameter, circumference, area);
      //expected results
      printf("Expected:diameter: 2.00, circumference 6.28, area 3.14\n");
      //actual results
      printf("Actual:diameter: %.2f, circumference %.2f, area %.2f\n", diameter, circumference, area);
      //Test 3
      test=circleStatistics(-1, diameter, circumference, area);
      //expected results
      printf("Expected:Error\n");
      //actual results
      printf("Actual: diameter: %.2f, circumference %.2f, area %.2f\n",diameter, circumference ,area);
    
    
      int *y;
      int *w;
      int *d;
      //Testing convertTime
      //Test 1
      test=convertTime(0, y, w, d);
      //expected results
      printf("Expected: Years 0, Weeks 0, Days 0\n");
      //actual results
      printf("Actual: Years %d, Weeks %d, Days %d\n", y, w, d);
      //Test 2
      test=convertTime(1000, y, w, d);
      //expected results
      printf("Expected: Years 2, Weeks 38, Days 4\n");
      //actual results
      printf("Actual: Years %d, Weeks %d, Days %d\n", y, w, d);
      //Test 3
      test=convertTime(-20, y, w, d);
      //expected results
      printf("Expected: Error\n");
      //actual results
      printf("Actual: Years %d, Weeks %d, Days %d\n", y, w, d);
      
      double *dilatedTime;
      //Testing lorentzTimeDilation
      //Test 1
      test=lorentzTimeDilation(1.0, 0.0, dilatedTime);
      printf("Expected: Dilated Time 1.0\n");
      printf("Actual: Dilated Time %.2f\n", dilatedTime);
      //Test 2
      test=lorentzTimeDilation(1.0, 0.25, dilatedTime);
      printf("Expected: Dilated Time 1.032\n");
      printf("Actual: Dilated Time %.2f\n", dilatedTime);
      //Test 3
      test=lorentzTimeDilation(1.0, -10, dilatedTime);
      printf("Expected: Error\n");
      printf("Actual: Dilated Time %.2f\n", dilatedTime);
      
      //Testing agm
      //test 1;
      double mean;
      mean=agm(1.0,2.0,.1);
      printf("Expected: ~= 1.5\n");
      printf("Actual: %.2f \n",mean);
      //test 2
      mean=agm(0.0,0.0,.1);
      printf("Expected: ~= 0\n");
      printf("Actual: %.2f \n",mean);
      //test 3
      mean=agm(-1.0,1.0,.1);
      printf("Expected: ~= 0\n");
      printf("Actual: %.2f \n",mean);
    
    
      return 0;
    };
    
    Any help would be apreciated. If you have questions about the code just ask, i'm not sure I did the best job explaining, sorry. Thanks!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Look at the following snippet:

    Code:
    int main(int argc,char** argv) {
      int test;
      double *diameter;
      double *circumference;
      double *area;
      //Testing CircleStatistics
      //Test 1
      test=circleStatistics(0.0, diameter, circumference, area);
    Where do you ever allocate memory for those pointers?
    You probably should have:


    Code:
    int main(int argc,char** argv) {
      int test;
      double diameter;
      double circumference;
      double area;
      //Testing CircleStatistics
      //Test 1
      test=circleStatistics(0.0, &diameter, &circumference, &area);
    Note the lack of the pointer (*) in the variable definition and the addition of the ampersand (&) in the function call, indicating you're passing the address of the variable to the function.

    Jim

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    88
    The calculation of *d at line 57 is wrong
    HINT what should *d be for 366 total days, ? for 365 total days ?

  4. #4
    Registered User
    Join Date
    Oct 2014
    Posts
    21
    Okay I was a little bit messed up with the pointers, and the day calculation was wrong. I will make changes to those things and see what happens. Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-11-2012, 01:03 AM
  2. Replies: 4
    Last Post: 12-11-2011, 04:25 PM
  3. small programming job VCPP / Object Oriented Programming
    By calgonite in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 01-04-2006, 11:48 PM
  4. Total newb to programming here... Question about the many programming languages. Ty!
    By tsubotakid1 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-05-2003, 10:32 AM