Thread: C Programming for sine waves drawing

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    14

    C Programming for sine waves drawing

    I am writing a C program to display the summation of a number of sine waves, which may be of differing phase, amplitude and frequency. The characteristics of each component wave should be supplied by the user and held in a linked-list as well as the wave resulting from the summation of all the entries. It should be possible for user to:

    1. add componet waves
    2. delete componet waves
    3.edit component wave characteristics

    The resulting wave should reflect the contents of the link-list at all times.

    *Welcome any ideas or code for this program, I will be constantly update the progress on this program and upload code as well as comments.
    Last edited by pattop; 04-25-2004 at 02:13 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So what have you done so far?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Just have a linked list of structs that contain the wave information. Then iterate through the list, adding up all the functions and getting your value for the given independent variable. This program is pretty simple.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    14

    Link list code

    Just finished writing the first part of the program. This is the database of the sine wave program basic on LINKED LIST, it is partially working, but for some reasons the program would crash while running the delete function, hope someone can help to solve this problem, I reckon it might to do to some variables, but I have already checked many times.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <dos.h>
    #include <windows.h>
    
    typedef struct data_type               // declare structure for sine_wave database
    {
            double amp;
            double freq;
            double phase;
            struct data_type *next;
            struct data_type *former;
    } DATA;
    
    DATA *current, *first, *previous, *following;
    
    void print_data(DATA *item)             // function to print current SINE-WAVE details
    {                                     // to screen
    
    	if (item==NULL)
    	{
    		gotoxy(20,5);
    		printf("                                        ");
    		gotoxy(20,5);
                    printf("       END OF THE SINE WAVE LIST        ");
    	}
    	else
    	{
    		gotoxy(20,5);
    		printf("                                        ");
    		gotoxy(20,5);
                    printf("            SINE WAVE DATA              ");
    
                    gotoxy(25,10); printf("Amplitude:                           ");
                    gotoxy(25,11); printf("Frequency:                       Hz  ");
                    gotoxy(25,12); printf("Phase:                           deg ");
    
                    gotoxy(40,10); printf("%9.2lf", item->amp);
                    gotoxy(40,11); printf("%9.2lf", item->freq);
                    gotoxy(40,12); printf("%9.2lf", item->phase);
    	}
    }
    
    DATA * add_data(DATA *item)              // add a SINE_WAVE to the end of the linked list
    {
            DATA *new_data;
    
    	gotoxy(20,5);
            printf("             Adding a new wave               ");
    
    
            new_data = (DATA *) malloc(sizeof(DATA));
            new_data->next = NULL;
    
    
            gotoxy(25,10); printf("Amplitude:                           ");
            gotoxy(25,11); printf("Frequency:                       Hz  ");
            gotoxy(25,12); printf("Phase:                           deg ");
            
            gotoxy(40,10); scanf("%lf", &new_data->amp);
            gotoxy(40,11); scanf("%lf", &new_data->freq);
            gotoxy(40,12); scanf("%lf", &new_data->phase);
    
    	if (item != NULL)
            {              
                    item->next = new_data;
    	}
    
            return (new_data);
    }
    
    DATA * del_data(DATA *item)             // delete data from list
    {
            DATA *previous, *following;
            if (item!=NULL)                 //ensure item exists to delete
            {
            previous = item->former;        //assign former and next
            following = item ->next;        //linksto pointers accordingly
    
            if (previous!=NULL) previous -> next=following;            //reroute around
               
            if (previous!=NULL) following -> former=previous;
    
            free(item);                     //release the item's memory
    
            if (previous!=NULL) item=previous;
            else item=following;
    }
    	return(item);
    }
    
    DATA * browse(DATA *item)              // move to next item in list
    {
    
    	if (item != NULL)
    	{
    		item = item->next;
    	}
    
    	return (item);
    }
    
    
    DATA * find(DATA *item)             // locate a particular item in the list
    {
    		gotoxy(20,5);
    		printf("      Facility not implemented yet!     ");
    
    		Sleep(1000);
    
    		return (item);
    }
    
    
    int main (void)                       // main function
    {
            DATA *first=NULL, *current=NULL, *last=NULL;
    	char response;
    
    	system("cls");
    
    	do                                 // repeat until "q" hit
    	{
    
                    print_data(current);             // print current item in list to screen
    
                    gotoxy(25,19); printf("'d': Delete current inputted data");   // display command options
                    gotoxy(25,18); printf("'a': Adding a new sine wave data");
                    gotoxy(25,20); printf("'f': find");
    		gotoxy(25,21); printf("'w': brouwse the database");
    		gotoxy(25,22); printf("'r': return to start of list");
    		gotoxy(25,23); printf("'q': quit the database");
    
    		gotoxy(40,25); putch(' ');      // overwrite old command
    		gotoxy(25,25);
    		printf("Enter command: ");      // get new command
    		response = getch();
    		gotoxy(34,40); putch(response); // print new command to screen
    
    		switch (response)               // call relevent function
    		{
                            case 'd': current = del_data(current); break;
                            case 'a': last = add_data(last); break;
                            case 'f': current = find(first); break;
                            case 'w': current = browse(current); break;
    			case 'r': current = first; break;
    		}
    
    		if (first == NULL) current = first = last;  // assign pointers to first
    									  // item provided in list
    	} while (response != 'q');
    
    	system("cls");
    
    	return 0;
    
    }
    Last edited by pattop; 05-12-2004 at 01:48 PM.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > gotoxy(34,10); scanf("%5.2lf", new_data->amp);
    > gotoxy(34,11); scanf("%9.2lf", new_data->freq);
    > gotoxy(34,12); scanf("%5.2lf", &new_data->phase);

    Try:
    Code:
            gotoxy(34,10); scanf("%lf", &new_data->amp);
            gotoxy(34,11); scanf("%lf", &new_data->freq);
            gotoxy(34,12); scanf("%lf", &new_data->phase);

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    127
    Quote Originally Posted by pattop
    Cheers, it seems to working OK now.

    btw, does anyone know how to program delete function and edit current record function for such linked list database?
    Editing a record is simple. All you need to do is prompt for the new data and assign it to the record. Deleting a node in a singly linked list isn't terribly complicated, but you do need to keep in mind that you must have a reference to the previous node so that the structure can be fixed.

    The idea is to walk through the list until the next node is the one you want to delete, then save it and set the current node's next pointer to the saved node's next pointer. This effectively removes the node.
    Code:
    a -> b -> c
    
    a    b -> c
    |         |
    -----------
    Once this is done, you can safely release the memory for b. Remember that you have several special cases. If the list contains no items then you obviously have nothing to remove. If the node to remove is at the head of the list, you need to reseat the head. And if the node wasn't found, you shouldn't remove or free anything. The code would look something like this.
    Code:
    node *remove_list(node *list, int item)
    {
      node *it, *save = NULL;
    
      if (!list) { /* Empty list */
        return list;
      }
      if (list->item == item) { /* Found as first node */
        save = list;
        list = list->next;
      }
      else {
        it = list;
        while (it->next && it->next->data != item) {
          it = it->next;
        }
        if (!it->next) { /* Not found */
          return list;
        }
        else { /* Found */
          save = it->next;
          it->next = save->next;
        }
      }
      free(save);
    
      return list;
    }

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    127
    >can anyone see why this doesnt work?
    A quick glance and test run both turn up nothing. Can you be more specific about how it isn't working?

  8. #8
    Registered User
    Join Date
    May 2004
    Posts
    37
    Quote Originally Posted by Kip Neuhart
    >can anyone see why this doesnt work?
    A quick glance and test run both turn up nothing. Can you be more specific about how it isn't working?
    sorry, it actually works

  9. #9
    Registered User
    Join Date
    Apr 2004
    Posts
    14
    Thanks for the reply of codse. I am kind of understanding how the delete function work, so I have adapted your code for my program in order to get it working.
    Last edited by pattop; 05-13-2004 at 03:44 AM.

  10. #10
    Registered User
    Join Date
    Apr 2004
    Posts
    14
    After programming the del function, did a compling of the codes, it seems fine on the compling, but everytime that the 'del_data' function runs, it tend to crash. Can someone figure out why, I have been trying to find out the problem for a whole night, I reckon is a variable or floating point got confused, but still not sure.

    Code:
    DATA * del_data(DATA *item)             // delete data from list
    {
            DATA *previous, *following;
            if (item!=NULL)                 //ensure item exists to delete
            {
            previous = item->former;        //assign former and next
            following = item ->next;        //linksto pointers accordingly
    
            if (previous!=NULL) previous -> next=following;            //reroute around
               
            if (following!=NULL) following -> former=previous;
    
            free(item);                     //release the item's memory
    
            if (previous!=NULL) item=previous;
            else item=following;
    }
    	return(item);
    }
    P.S. I have also updated the original sort code. Please refer to it. Thanks

  11. #11
    Registered User
    Join Date
    Apr 2004
    Posts
    14

    Thanks

    Thanks Kip Neuhart, just finished the edit function, as you say it pretty simple, cheers

  12. #12
    Registered User
    Join Date
    Apr 2004
    Posts
    14
    Quote Originally Posted by swoopy
    > gotoxy(34,10); scanf("%5.2lf", new_data->amp);
    > gotoxy(34,11); scanf("%9.2lf", new_data->freq);
    > gotoxy(34,12); scanf("%5.2lf", &new_data->phase);

    Try:
    Code:
            gotoxy(34,10); scanf("%lf", &new_data->amp);
            gotoxy(34,11); scanf("%lf", &new_data->freq);
            gotoxy(34,12); scanf("%lf", &new_data->phase);

    Thanks Swoopy, I guess I didn't realize that 'lf' can use it on its own without specific length, "School Boy Error", lol, Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing HBITMAP into CWnd, Acquired from "screenshot"
    By DeusAduro in forum Windows Programming
    Replies: 6
    Last Post: 07-02-2009, 03:41 PM
  2. Slow drawing code
    By tjpanda in forum Windows Programming
    Replies: 5
    Last Post: 05-09-2008, 05:09 PM
  3. Line Drawing Algorithm
    By Axpen in forum Game Programming
    Replies: 15
    Last Post: 08-01-2005, 06:30 PM
  4. How to do double buffing with win32 drawing?
    By Josh Kasten in forum Windows Programming
    Replies: 2
    Last Post: 03-27-2004, 12:02 AM
  5. drawing minimaps and radar screens.
    By Eber Kain in forum Game Programming
    Replies: 4
    Last Post: 03-08-2002, 11:44 AM