Welcome to Dream.In.Code
Become a C++ Expert!

Join 136,909 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,746 people online right now. Registration is fast and FREE... Join Now!




Having trouble with program

 
Reply to this topicStart new topic

Having trouble with program, Finding smallest consecutive numbers

hogben45
31 Mar, 2008 - 04:16 PM
Post #1

New D.I.C Head
*

Joined: 29 Jan, 2008
Posts: 25

Hi everybody I need some help with my current program. I am supposed to identify the lowest two consecutive numbers in the sorted array with the smallest difference between them, along with other functions as you can see. I am having trouble even beginning to write this part of the code. Any tips or advice would be greatly appreciated!! Thanks in advance.

CODE


#include <stdio.h>
#include <math.h>

void average( double Z[] , int n , double *avg );
void stats( double Z[] , int n , double avg , double *variance, double *std_dev );
void descending( double Z[] , int r );
void median( double Z[] , int n , double *med);

main ()

{

double Z[100];
int n , r , flag;
double avg , med;
double variance , std_dev;                            

FILE *infile;

printf("\n\nThis program is the work of Ben Hogan, ID #211");
printf("\n'program_5_v_4.c'           Due 3/31/08");

infile = fopen("program_5_2.dat" , "r");

r = 0.;

flag = fscanf(infile ,"%lf" , &Z[r]);  

while( 1 == flag ) {


while ( Z[r] > 0. ) {

        r = r + 1;

        flag = fscanf(infile ,"%lf" , &Z[r]);  

        }        

n = r;

descending( Z , r );
average( Z , n , &avg );
stats( Z , n , avg , &variance , &std_dev );
median( Z , n , &med);

printf("\nAverage   = %9.2f\n" , avg );

printf("Median    = %9.2f\n\n" , med );

printf("Standard Deviation =  %.2f\n\n" , std_dev );

printf("The number of entries in the array is: %d \n\n" , n );

}

system("pause");

}

/**************** End of Main ***************************************/

/************ Start of Descending **********************************/

void descending( double Z[] , int r  )

{

int j , k , index_next_smallest , n;
double temp , next_smallest;

FILE *infile;

infile = fopen("program_5_2.dat" , "r");

for (r = 0; Z[r] > 0.; r = r + 1 ) {
    
    fscanf(infile, "%lf" , &Z[r]);
        
    }

    n = 100;

    for(j = 0; j < n - 1; j = j + 1) {

        next_smallest = Z[j];

        index_next_smallest = j;
    
        for (k = j; k < n; k = k + 1) {
    
            if (Z[k] > next_smallest) {
        
                next_smallest = Z[k];
            
                index_next_smallest = k;
            
            }
        }
        
    temp = Z[j];
    
    Z[j] = Z[index_next_smallest];
    
    Z[index_next_smallest] = temp;        

    }    

printf("\n\nVALUES SORTED: \n\n");

for (r = 0; Z[r] > 0.; r = r + 1 ) {
    
    printf("%g\n" , Z[r] );        
    
}

    
}

/******************* End of descending *****************************/

/****************** Start of  average ******************************/
  
void average( double Z[] , int n , double *avg )
{

double sum;
int      r;

sum = 0.;

for( r = 0; r < n; r = r + 1 ) {

    sum = sum + Z[r];    

}

(*avg) = sum / n;

}
/******************** End of average *********************************/

/******************** Start of stats *********************************/

void stats( double Z[] , int n , double avg , double *variance, double *std_dev )
{

int r;
double var;

var = 0.;

for( r = 0; r < n; r = r + 1 ) {

    var = var + pow( (Z[r] -  avg ) , 2. );    

}

(*variance) = var / ( n - 1. );

(*std_dev) = sqrt( (*variance) );

}
/************* End of stats ******************************/

/************ Start of median ****************************/

void median( double Z[] , int n , double *med )
    
    {
    
/* If n is even */
    
    if (n % 2 == 0 ) {
    
        (*med) = ( Z[n / 2 ] + Z[n / 2 - 1]) / 2.;
    
        }
    
/* If n is odd */
        
    else {
    
        (*med) = Z[ n / 2 ];
        
        }

    }    

/*********************** End of median *******************/


P.S. I have attached the data file. Thanks!!!


Attached File(s)
Attached File  program_5_1.txt ( 36bytes ) Number of downloads: 6
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Having Trouble With Program
1 Apr, 2008 - 10:33 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,231



Thanked: 220 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Do you still need help with this or have you turned it in already? I noticed it is due March 31 which was yesterday. I didn't want to waste my time if the answer is no good to you anymore.

smile.gif
User is offlineProfile CardPM
+Quote Post

hogben45
RE: Having Trouble With Program
1 Apr, 2008 - 01:44 PM
Post #3

New D.I.C Head
*

Joined: 29 Jan, 2008
Posts: 25

Thank you so much Martyr2...The program is actually due tomorrow (April 2)...So any help would be greatly appreciated with finding the smallest consecutive difference. Thanks!

Edit: I have completed the program...Thanks.

This post has been edited by hogben45: 2 Apr, 2008 - 08:44 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 09:55PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month