Thread: Help. Beginner Programmer

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    12

    Help. Beginner Programmer

    Code:
    #include <stdio.h>
    
    int main()
    {
    	float Tarray[] = {0, 5, 10, 20, 30, 40, 50, 60, 70, 80, 100, 120, 140, 160, 180, 230, 280, 380};
    	float Varray[] = {.21, .30, .37, .45, .49, .50, .49, .47, .45, .43, .37, .33, .29, .25, .19, .13, .08, .04};
    	float Vavg[17];
    	float Tperiod[17];
    	float Vcalc[17];
    	float Sum = 0;
    	float TavgV = 0;
    	
    	int i;
    	for(i=0;i<18;i++){
    		Vavg[i] = (Varray[i] + Varray[i+1])/2;
    	}
    	
    	for(i=0;i<18;i++){
    		Tperiod[i]  = Tarray[i+1] - Tarray[i];
    		Vcalc[i] = Tperiod[i]*Vavg[i];
    		Sum += Vcalc[i];
    	}
    	
    	TavgV = Sum/(Tarray[17] - Tarray[0]);
    	
    	printf("The time averaged voltage is", TavgV);
    	return (0);
    }
    When I compile this code in Quincy 2005, I get "The time averaged voltage is" with calculated value for TavgV missing.
    Can anyone spot the error?

    Also: Side note. Does anyone know what the concept of "the worst possible values" means? I need to create/explain a program in C/Python demonstrating them

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your loops are running beyond the array boundary. Vavg[17] has elements from 0 to 16 only, etc. Then you're also using i+1 inside the loop which is going out of bounds -- also bad!

    Cut down the top of i by two, and adjust your logic, if necessary.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>printf("The time averaged voltage is", TavgV);
    printf isn't psychic unlike other languages such as C++ that can see you've passed arguments to them. No, printf requires you to explicitly tell it you have passed it an argument and what type it is in your format string. So it should be
    printf("The time averaged voltage is %f.", TavgV);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I suggest you better use python if you've no previous experience with C.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    12
    Thanks everyone! I fixed the printf statement and the array boundaries and it works now. I'm not sure if the time averaged voltage is a little off through. It gave me about .21, but i got about .22 in my calculator using lists.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Floating arithmetic is very inaccurate on computers (unless you use a mathematical library; but they're vastly slower). So what you get is probably a consequence of that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Why don't you use double?

  8. #8
    Registered User
    Join Date
    Aug 2010
    Posts
    12

    printing array help

    Can someone show me how to edit the program so that Tarray, Varray, Vavg, and Tperiod will print?
    Btw the most recent version is:
    Code:
    #include <stdio.h>
    
    int main()
    {
    	float Tarray[] = {0, 5, 10, 20, 30, 40, 50, 60, 70, 80, 100, 120, 140, 160, 180, 230, 280, 380};
    	float Varray[] = {.21, .30, .37, .45, .49, .50, .49, .47, .45, .43, .37, .33, .29, .25, .19, .13, .08, .04};
    	float Vavg[17];
    	float Tperiod[17];
    	float Vcalc[17];
    	float Sum = 0;
    	float TavgV = 0;
    	
    	int i;
    	for(i=0;i<18;i++){
    		Vavg[i] = (Varray[i] + Varray[i+1])/2;
    	}
    
    	
    	for(i=0;i<18;i++){
    		Tperiod[i]  = Tarray[i+1] - Tarray[i];
    		Vcalc[i] = Tperiod[i]*Vavg[i];
    		Sum += Vcalc[i];
    	}
    	
    	TavgV = Sum/(Tarray[17] - Tarray[0]);
    	
    	printf("The time averaged voltage is %f", TavgV);
    	
    	return (1);
    }
    Last edited by n4rush0; 08-25-2010 at 12:44 AM.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    An array consists of N elements, so print N elements. You've already learned how to use loops to access the individual elements of an array.
    So instead of assigning them, print them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Aug 2010
    Posts
    12
    I tried to do it like this:

    Code:
    #include <stdio.h>
    
    int main()
    {
    	float Tarray[] = {0, 5, 10, 20, 30, 40, 50, 60, 70, 80, 100, 120, 140, 160, 180, 230, 280, 380};
    	float Varray[] = {.21, .30, .37, .45, .49, .50, .49, .47, .45, .43, .37, .33, .29, .25, .19, .13, .08, .04};
    	float Vavg[17];
    	float Tperiod[17];
    	float Vcalc[17];
    	float Sum = 0;
    	float TavgV = 0;
    	
    	int i;
    	for(i=0;i<18;i++){
    		printf("Vales for time in ms: %f", Tarray[i]);
    	}
    	
    	int i;
    	for(i=0;i<18;i++){
    		printf("\nVales for voltage in microvolts: %f", Varray[i]);
    	}
    	
    	
    	int i;
    	for(i=0;i<18;i++){
    		Vavg[i] = (Varray[i] + Varray[i+1])/2;
    	}
    	
    	int i;
    	for(i=0;i<17;i++){
    		printf("\nAverage volrage within each time period: %f", Vavg[i]);
    	}
    	
    	
    	for(i=0;i<18;i++){
    		Tperiod[i]  = Tarray[i+1] - Tarray[i];
    		Vcalc[i] = Tperiod[i]*Vavg[i];
    		Sum += Vcalc[i];
    	}
    	
    	printf("\nThe sum is: %f", Sum)
    	
    	TavgV = Sum/(Tarray[17] - Tarray[0]);
    	
    	printf("\nThe time averaged voltage is %f", TavgV);
    	
    	return (1);
    }

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    So is there a problem?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Aug 2010
    Posts
    12
    i get these errors:

    18: redeclaration of 'i' with no linkage
    13: previous declaration of 'i' was here
    18: ISO C90 forbids mixed declarations and code
    24: redeclaration of 'i' with no linkage
    18: previous declaration of 'i' was here
    24: ISO C90 forbids mixed declarations and code
    29: redeclaration of 'i' with no linkage
    24: previous declaration of 'i' was here
    29: ISO C90 forbids mixed declarations and code
    43: expected ';' before tavgV

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You're trying to define the variable "i" several times. You can only create a variable once, unless the newer one is in a deeper block.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Aug 2010
    Posts
    12
    Okay, here's what I have now:
    Code:
    #include <stdio.h>
    
    int main()
    {
    	float Tarray[] = {0, 5, 10, 20, 30, 40, 50, 60, 70, 80, 100, 120, 140, 160, 180, 230, 280, 380};
    	float Varray[] = {.21, .30, .37, .45, .49, .50, .49, .47, .45, .43, .37, .33, .29, .25, .19, .13, .08, .04};
    	float Vavg[17];
    	float Tperiod[17];
    	float Vcalc[17];
    	float Sum = 0;
    	float TavgV = 0;
    
    	int i;
    	for(i=0;i<18;i++){
    		Vavg[i] = (Varray[i] + Varray[i+1])/2;
    	}
    	
    	for(i=0;i<18;i++){
    		print("Average Voltage with each time period: %f", Vavg[i]);
    	}
    	
    	
    	
    	for(i=0;i<18;i++){
    		Tperiod[i]  = Tarray[i+1] - Tarray[i];
    		Vcalc[i] = Tperiod[i]*Vavg[i];
    		Sum += Vcalc[i];
    	}
    	
    	printf("\nThe sum is: %f", Sum);
    	
    	TavgV = Sum/(Tarray[17] - Tarray[0]);
    	
    	printf("\nThe time averaged voltage is %f", TavgV);
    	
    	return (1);
    }
    for:
    Code:
    	for(i=0;i<18;i++){
    		print("Average Voltage with each time period: %f", Vavg[i]);
    	}
    i'm still getting an error of an undefined print statement

  15. #15
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Supposed to be printf, isn't it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner programmer
    By Kool4School in forum C Programming
    Replies: 16
    Last Post: 11-24-2008, 01:16 PM
  2. beginner c++ programmer compile errors
    By dodo10 in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2008, 04:37 PM
  3. Beginner Programmer
    By silverjump in forum C++ Programming
    Replies: 2
    Last Post: 04-04-2005, 06:03 PM
  4. Beginner c++ programmer looking for suggestions....
    By Sheshi in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2003, 04:38 PM
  5. Any beginner C++ programmer wants to.....
    By incognito in forum C++ Programming
    Replies: 5
    Last Post: 12-06-2001, 08:15 AM