[ create a new paste ] login | about

Link: http://codepad.org/ganCKNPr    [ raw code | output | fork ]

C, pasted on Dec 9:
/*----------------------------------------------*/
/* Array function V1.0                          */
/* Writen by J.S-R. Date Dec 8 2012             */
/* Purpose:                                     */
/*      This program computes the repition      */
/*      of number points inside a set of data.  */
/*      read from a data file whose path is     */
/*      specified within the header section     */
/*      of the main.cpp file.                   */
/*----------------------------------------------*/
#include <stdio.h>
#include <math.h>
#define FILENAME1   "/datapoints.txt"
#define Nrows 100

/* declare and instalize global variables */
int np[Nrows];
int copystor[Nrows];


int main(void)
{
    /* declare local variables */
    int count,i,dec,ec1;
    FILE *datapoints;
    void copy(int cp[], int count);
    void sort(int cp[], int count);
    char yn;


    
    /*instalize local variable */
    count = i = 0;

    /* open file and scan data into array if file does not exist, */ 
    /* print error and allow data points to be entered by hand.   */
    datapoints = fopen(FILENAME1,"r");
    if(datapoints == NULL)
    {
        printf("error opening file\n");
        printf("if you would like to enter the data points by hand, please enter the number of \n");
        printf("data points you have or enter a negitive number if you would like to quit the program\n");
        scanf("%i",&dec);
        
        if(dec > 0)
        {
            if(dec <= Nrows)
                for(i = 0; i<=dec-1; i++)
                {
                    printf("please enter data point number %i \n",i+1);
                    scanf("%i",&np[i]);
                }
            else
                printf("The maximum number of data points you may enter is %i,\n you chose to enter %i which is to many \n",Nrows,dec);
            if(dec <= 0)
                printf("Exiting program");
            count = dec;
        }
        
    }
    else
    {
        while(fscanf(datapoints,"%i",&np[i])==1)
            i++;
        count = i;
    }
    
    
    /* copy and sort array */
    printf("would you like to back up your array? \n");
    getchar();
    yn = getchar();
    if(yn == 'y')
        copy(np,count);
    else
        printf("your array is not being backed up \n");

    /* copy check */
    printf("would you like to check if your array was copyed correctly? \n");
    printf("press y to check or press n to skip check \n");
    getchar();
    yn = getchar();
    do
    {
    if(yn == 'y')
        for(i=0; i<=count-1; i++)
        {
            printf("%i %i \n",np[i],copystor[i]);
            ec1 = 1;
        }
    else if(yn == 'n')
    {
        printf("not checking array \n");
        ec1 = 1;
    }
    else
    {
        printf("invalid entry please enter y to copy or n to not copy your array");
        getchar();
        yn = getchar();

    }
    }while(ec1 != 1);
    
    sort(copystor,count);
    
    /* sort array */
    /*
    printf("would you like to sort your array? \n");
    printf("press y to sort or press n to skip sort \n");
    getchar();
    yn = getchar();
    if(yn == 'y')
        sort(copystor,count);
    else
        printf("your array is not being backed up \n");

    */
    printf("sorted array\n");
    for(i=0; i<=count-1; i++)
    {
        printf("%i \n",copystor[i]);
    }
    
    printf("This program was written by J.S-R \n");
    printf("This projects name is arrayfunction \n");
    return 0;
    

}
/*-----------------------------*/
/* Function copy array                      */
/* This function copys one array to another */

void copy(int np[], int count)
{
    /* instalize local variables */
    int k;
    
    for(k = 0;k<= count; k++)
        copystor[k] = np[k];
    return;
}
/*-*/
/* This function sorts a 1-D array */
void sort(int copystor[], int count)
{
    /* declare and installize local variable */
    int k, j, store;
    
    for(k=0; k<=count; k++)
    {
        store = copystor[k];
        for(j=0; j<=count; j++)
        {
            if(copystor[j] < store)
                copystor[k] = store;
        }
    }
    return;
}


Output:
1
Segmentation fault


Create a new paste based on this one


Comments: