User Tools

Site Tools


user:bkenne11:portfolio:eoceprime

Project: YOUR PROJECT NAME HERE

A project for Data structures by Brandon Kennedy during the Fall 2011 Semester.

This project was begun on 12-7-2011 and is anticipated to take 1 day.

Objectives

The purpose of this project is to pass data structures with an A, no joke. I need some more attributes, so i am writing up a project that will check for the primality of numbers.

Prerequisites

In order to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved:

  • the C programming guide
  • c pocket reference
  • matt haas
  • dinosaurs

Background

The purpose of this project is to better understand some of the math capabilities of the C programming language, and to develop better skills for merging programming with math. What better way than to find prime numbers!

Scope

This project will be specifically implemented to fill two stacks. A stack of prime numbers, and a stack of composite numbers. These will be filled with numbers based on a numeric algorithm that will test for primality.

Attributes

State and justify the attributes you'd like to receive upon successful approval and completion of this project.

  • attribute1: Pointers → This program will use pointers to nodes.
  • attribute2: stack → this program will implement a stack
  • attribute3: doubly linked list → This program uses a doubly linked list to connect the stacks nodes
  • attribute4: file I/0 → this program will record to a file
  • attribute5: terminal I/0 → this program will output to the terminal → used for systems attribute

Code

main

#include "stack.h"
#include <math.h>
int main()
{
        FILE *fptr;
        double input = 0;//user input
        int root=0,i=0,j=0,num=0,flag=0;
        Stack *prime;//prime number stack
        Stack *comp;//prime number stack
        prime = (Stack *)malloc(sizeof(Stack));
        comp = (Stack *)malloc(sizeof(Stack));
        comp -> top = comp -> bottom = comp -> tmp = NULL;
        prime -> top = comp -> bottom = comp -> tmp = NULL;
        fptr = fopen("primerecords", "w");
        printf("Enter a number to test primality up to(from 2 to): ");
        scanf("%lf", &input);
        fprintf(fptr, "Testing primality of all numbers up to %lf\n" , input);
        num = (int)(input);

        for(i=2;i<num;i++)
        {
                root = (int)(sqrt(i));
                flag = 0;
                for(j = 2; j <= root; j++)
                {
                        if((i % j) == 0)
                        {
                                if((i != 2) && (i !=  3))
                                {
                                        flag = 1;
                                        comp = push(comp, i); // load i into composite list
                                }
                        break;
                        }
                }
                if(flag == 0)
                        prime = push(prime, i); //load i into prime list
        }
        prime -> tmp = prime -> top;
        printf("Primes:");
        fprintf(fptr, "Primes: ");
        while(prime -> tmp != NULL)
        {
                fprintf(fptr, ". %d", prime -> tmp -> value);
                printf(". %d", prime -> tmp -> value);
                prime -> tmp = prime -> tmp -> next;
        }
        printf("\n");
        fprintf(fptr, "\n");
        comp -> tmp = comp -> top;
        printf("\nComposites:");
        fprintf(fptr, "\nComposites:");
        while(comp -> tmp != NULL)
        {
                fprintf(fptr, ". %d", comp -> tmp -> value);
                printf(". %d", comp -> tmp -> value);
                comp -> tmp = comp -> tmp -> next;
        }
        printf("\n\n");
        fprintf(fptr, "\n");
        fclose(fptr);
        return 0;
}

push

#include"stack.h"
 
Stack *push(Stack *notes, int val)
{
        Node *stick;
        stick=(Node *)malloc(sizeof(Node));
        stick -> value = val;
 
        if(notes -> top == NULL)
                notes -> top = notes -> bottom = stick;
        else
        {
                notes -> top -> prev = stick;
                stick -> next = notes -> top;
                notes -> top = notes -> top -> prev;
        }
        return notes;
}

stack.h

#ifndef _STACK_H_
#define _STACK_H_
#include"linkedlist.h"
 
struct stack{
        struct node *top;
        struct node *bottom;
        struct node *tmp;
};
typedef struct stack Stack;
 
int peek(Stack *);
Node *pop(Stack *);
Stack *push(Stack *, int);
 
#endif

linkedlist.h

#ifndef _LINKED_LIST_H
#define _LINKED_LIST_H
#include<stdlib.h>
#include<stdio.h>
 
struct node {
        char value;
        struct node *next;
        struct node *prev;
};
typedef struct node Node;
 
struct list{
        struct node *start;
        struct node *end;
        struct node *tmp;
};
typedef struct list List;
 
List *copy(List *);
List *append(List *, Node *, int);
List *insert(List *, Node *, int);
Node *remov(List *, Node *);
void deletelist(List *);
Node *find(List *, int);
 
#endif

Execution

lab46:~/src/DataS/EoCE/0x3$ ./prime Enter a number to test primality up to(from 2 to): 50 Primes:. 47. 43. 41. 37. 31. 29. 23. 19. 17. 13. 11. 7. 5. 3. 2

Composites:. 49. 48. 46. 45. 44. 42. 40. 39. 38. 36. 35. 34. 33. 32. 30. 28. 27. 26. 25. 24. 22. 21. 20. 18. 16. 15. 14. 12. 10. 9. 8. 6. 4

lab46:~/src/DataS/EoCE/0x3$

Reflection

I finish programs WAY to quickly… I finished this program with a math algorithm that worked great! up to a certain number. Because I figured it out in my head, wrote it down, tested it with low numbers, and was done. That is a horrible idea, fyi. Always plan out your logic and put it through intense testing. If it has any gaps or weak spots try to reason then out or put cases in to avoid any flaws.

user/bkenne11/portfolio/eoceprime.txt · Last modified: 2011/12/14 22:05 by bkenne11