# Help. Beginner Programmer

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 08-22-2010
n4rush0
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
• 08-22-2010
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.
• 08-22-2010
Elysia
>>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);
• 08-22-2010
Bayint Naung
I suggest you better use python if you've no previous experience with C.
• 08-22-2010
n4rush0
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.
• 08-22-2010
Elysia
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.
• 08-22-2010
Bayint Naung
Why don't you use double?
• 08-25-2010
n4rush0
printing array help
Can someone show me how to edit the program so that Tarray, Varray, Vavg, and Tperiod will print?
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); }```
• 08-25-2010
Elysia
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.
• 08-25-2010
n4rush0
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); }```
• 08-25-2010
Elysia
So is there a problem?
• 08-25-2010
n4rush0
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
• 08-25-2010
Elysia
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.
• 08-25-2010
n4rush0
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
• 08-25-2010
Bayint Naung
Supposed to be printf, isn't it.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last