# Basic C

• 12-04-2011
toddeeeEEee
Basic C
Hey, so basically coming towards the end of the first term at university, i've been set an assignment worth 40% of the years marks. Unfortunately the lecturer has been ill for the past few weeks, and i'm kinda stumped. I'm not going to ask for much help, but getting some advice and helping me understand a little would be a great help.

Here is a cut version of the assignment: assignment.docx

Thanks.
• 12-04-2011
CommonTater
Nobody here, in their right mind is going to open a doc or docx file over the internet.

Plus we don't do people's homework for them...

If you get into a problem with the code, post it here and we'll see what we can do.
• 12-04-2011
rags_to_riches
The assignment:

Quote:

Objective: To develop a C Program to calculate components of matching
attenuators.

Introduction: You are to develop a C program to help design resistive attenuation and matching
pads for a telephone line. Generally, telephone lines and electronic equipment need to be matched to
their source and loads to avoid reflection of the signals. Attenuators are used to reduce the level of
large signals that need to be fed to a more sensitive circuit.

Theory:
The two types of attenuator are Tee and Pi. Both are used extensively.
There are two types of each, balanced and unbalanced. Unbalanced type use a
Common earthed return, while balanced types do not.
The Tee arrangement can be used in its unbalanced form, or its balanced form

(Sometimes known as a balanced H network).

Figure 1: Tee Networks with equations.

Figure 2: Pi Network with equations.

Z1 is the impedance seen looking into the network, while Z2 is the impedance seen
looking into the output. So if the source (generator) has an output impedance of 600
And the load has an impedance of 75then the network will be arranged to have
Z1=600, and Z2=75.
Since the networks are all designed to introduce a loss (expressed in dB or Decibels)
then this is used, with the Z1, Z2 values to calculate the resistor values.

What you have to do is develop a program to ask the user what kind of
network she/he wants, what the input and output impedance's are to be, and what the loss is. The program should then calculate the value of the resistance values and display them, along with the other network parameters. You might like to start by designing the program for the Tee network first. Then go on to develop the Pi network. Test each as you develop it to avoid having a lot of error to deal with at one time. Finally, the program askes the user if they want to save the design to a file. This involves saving the resistor values, input and output impedances, and attenuator loss information to a file. Hint: Use a C struct for this so as to keep all the data together. You will need to do some research to complete this part of the assignment.

The complete program can be developed as a menu program with an option to terminate the program, or perform another calculation.

Test Case example:
Test Case 1: If Z1=600 ohms, Z2=900 ohms, an unbalanced Tee with a loss of 15db would have the following resistor values. R1=369.29 ohms, R2=688.89 ohms,
R3=269.88 ohms.
For other test cases, check your results by comparing with hand calculations.
• 12-04-2011
Alan Gott
In order for anyone to help, you have to have some work done. Do as much as you can, and when stumped on a specific spot, ask for help.
• 12-05-2011
toddeeeEEee
Yeah sorry I was in the library.

Basically I can do the remotely easy stuff, such as the menus. I'm having trouble with implicating the equations themselfs, and implimenting them into the code.

So far for my Tee network I have;

Code:

```/* Attenuator Calculator Program */ #include<stdio.h> #include<math.h> float Tee(void); /*Tee Network*/ float Pi(void); /*Pi Network*/ float Minimum_Loss(void); /* Minimum Loss*/ float Balanced(void); float Unbalanced(void);  main(void)    { char choice; printf("-----Welcome to the Attenuator calculator program----"); do{ printf("\n\n"); printf("Enter \n T=Tee Network \n P=Pi Network \n I=Minimum Loss \n Z=Quit\n"); choice=getchar(); switch(toupper(choice)) { case 'T': Tee();break; case 'P': Pi();break; case 'I': Minimum_Loss();break; case 'Z': printf("\nThank you for using the Attenuator calculator program!\n");break; default: printf("\nThere is no option for this letter; please try again\n"); break; } }while(choice != 'Z'&& choice != 'z'); } /* End of Menu */ /*-------------------- For Tee Network ---------------*/            float Tee(void){             char choice;    printf("\n\nIs the network balanced or unbalanced?");                        do{  printf("\n\n");  printf("Enter: \n B=Balanced Network \n U=Unbalanced Network\n Z=Quit");  fflush(stdin);    choice=getchar();   switch(toupper(choice))  {     case 'B': Balanced();break;     case 'U': Unbalanced();break;     case 'Z': printf("Thank you for using the attenuator calculator program!");break;     default: printf("There is no option for this letter, please try another letter");     }     }while(choice != 'Z'&& choice !='z');     }                 float Balanced(){         printf("Enter value for input impedance"); }     float Unbalanced(){         printf("Enter Value for input Impedance");         }```
Any help from here? Thanks.
• 12-05-2011
CommonTater
Well, for starts, your compiler should be complaining like crazy...
Referring to the code sample in message 5...

Line 7 ... you prototype a function that does not exist in your code.
Line 13 ... it's int main (void) ... and it returns an integer to the OS at the closing brace.
Line 23 to 27 ... you should not stack stuff up on a line like that, it's too easy to miss something.
Code:

```switch(toupper(choice))   {     case 'T':         Tee();         break;     case 'P':         Pi();         break;     case 'I':         Minimum_Loss();         break;     case 'Z':         printf("\nThank you for using the Attenuator calculator program!\n");         return 0;     default:         printf("\nThere is no option for this letter; please try again\n");   }```
Line 30 ... you are exiting main without returning an integer value; usually 0.
Line 37 ... you declare tee() to return a float but you never do return a float.
Line 48 ... fflush(stdin); is a non-standard use of fflush() and in many compilers can result in erratic behaviour. Don't use it.
Lines 51 to 56 ... see above...
Lines 62 and 67 ... you declare functions to return floats but never do return a value

As general commentary...
Your source code formatting needs serious work... and yes it does matter. Bad formatting makes code harder to read.
• 12-05-2011
toddeeeEEee
To be fair, this is my first attempt at a program besides the obvious 'Hello world' tutorial. I'm using Quincy 2005, and everything seems to be coming out without errors :/ ( Forgot to mention it has to be done through Quincy, if that makes a difference or not ). Sorry if I sound stupid here.
• 12-05-2011
CommonTater
Quote:

Originally Posted by toddeeeEEee
To be fair, this is my first attempt at a program besides the obvious 'Hello world' tutorial. I'm using Quincy 2005, and everything seems to be coming out without errors :/ ( Forgot to mention it has to be done through Quincy, if that makes a difference or not ). Sorry if I sound stupid here.

Ok then... you've done pretty good for your first program...