A program has following 3 parts
1). write a program which calculates the value of Y for valid inputs of -10<= X <=10for a function Y=10*sin(X*18).
2). An extension to this program which prints a line of 21 characters with an asterix "*" at the value of Y with the 0 position shown by "|" . When Y=0 a " *" has to be printed instead of |. (Note: At one particular value of Y only one line shud be printed i.e if Y=5 then
shud be printed only
if Y-=5 then
shud be printed only.
3). An extension to this which creates a 21-element array to calculate and hold 21 values of Y for the given function. Then using these values we have to print 21 "-" lines as shown. Here use of functions which pass parameters in/out shud be demonstrated.
I have got the code for first 2 parts which is as shown: and is working properly...
Code:
#include <hidef.h> /* common defines and macros */
#include <mc9s12dp512.h> /* derivative information */
#include <hidef.h> /* common defines and macros */
#include <mc9s12dp512.h> /* derivative information */
// NOTE MUST put this header file in beside main.c to use ascii/float
#include "term.h" /* functions to support simulator terminal */
#include <math.h>
#define LINE_LENGTH 21
#pragma LINK_INFO DERIVATIVE "mc9s12dp512"
void main(void)
{
char rx_msg[LINE_LENGTH]=""; //stores character array of length 21 for input
char tx_msg[LINE_LENGTH]="" ; // stores character array of length 21 for output
float xinradians,x,y;
int j, spaces, temp;
PeriphInit(); // Microcontroller initialization
EnableInterrupts;
sendmessage("\nenter value of x");
getmessage(rx_msg); // input the value of x
x = atoi(rx_msg); // convert character value of x to integer
if((x>=-10) && (x<=10))
{
xinradians = (x * (_M_PI/180)); // converting x into radians
y = 10 * sin(xinradians*18); // calculating value for y
ftoa(y,tx_msg); // converting y from floating to character
sendmessage("The value of y is");
sendmessage(tx_msg); // output the value of y
}
else
{
sendmessage("Invalid Number");
}
temp=y; //storing the value of y in integer variable temp
sendmessage("\n\n");
if(temp<0) //condition for temp values less than 0
{ // calculating the desired spaces
spaces=10-(temp*(-1)); // initialising counter variable j=0
j=0;
while(j<spaces) // loop for printing spaces
{
sendmessage(" ");
j++;
}
sendmessage("*"); // printing * after the spaces
spaces= 9-spaces; // calculating new value for spaces after *
j=0;
while(j<spaces) // loop to print spaces
{
sendmessage(" ");
j++;
}
sendmessage("|"); // printing | at 0th position
}
if(temp==0) // condition for temp value equal 0
{
spaces = 9 - temp; // calculating desired value of spaces
j=0;
while(j<spaces) // loop to print spaces
{
sendmessage(" ");
j++;
}
sendmessage("*"); // display * at temp=0
}
if(temp>0) // condition for temp values greater than 0
{
j=0;
while(j<10) // loop to display spaces
{
sendmessage(" ");
j++;
}
sendmessage("|"); // printing | at 0th position
spaces= temp -1; // calculating spaces after |
j=0;
while(j<spaces) // loop to print spaces after |
{
sendmessage(" ");
j++;
}
sendmessage("*"); // printing * after desired spaces
}
} //end main
The desired output which i got after first 2 parts is as shown:
Code:
* |
* |
* |
* |
* |
* |
* |
* |
* |
*|
*
|*
| *
| *
| *
| *
| *
| *
| *
| *
| *
I am stuck with the 3rd part. Some1 plz help me out with 3rd part. The final output after all 3 sections completed is as :
Code:
* |
* |
* |
* |
* |
* |
* |
* |
* |
*|
----------*----------
|*
| *
| *
| *
| *
| *
| *
| *
| *
| *