Sponsored Content
Top Forums Programming Representing INFINITY in C language???? Post 302599778 by gabam on Saturday 18th of February 2012 09:18:50 AM
Old 02-18-2012
Representing INFINITY in C language????

Hi friends,
I hope everybody is doing fine. I have written this small c program for the merge_sort algorithm. The algorithm that I am following uses the value infinity. My question is, how to use this infinite value in this c program? I haved used a very large value (99999) instead of infinity, but this value would cause the program to fail in case the unsorted array contains a value larger than 99999. Could you help me with thing? You can have a look at my code.

Code:
 
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
 
int A[] = {4, 2, 3, 1, 23, 15, 104, 8, 5};
void Merge_Sort(int [], int, int);
void Merge(int [], int, int, int);
 
int main()
{
    int i;
    printf("Unsorted array\n\n");
    for(i = 0 ; i < 9 ; i++)
    {
        printf("%d\n",A[i]);
    }
    Merge_Sort(A,0,8);
    printf("\nSorted array\n\n");
    for(i = 0 ; i < 9 ; i++)
    {
        printf("%d\n",A[i]);
    }
    return 0;
}
 
void Merge_Sort(int A[], int p, int r)
{
    int q;
    if(p < r)
    {
        q = floor((p + r)/2);
        Merge_Sort(A, p, q);
        Merge_Sort(A, q + 1, r);
        Merge(A, p, q, r);
    }
    return 0;
}
 
void Merge(int A[], int p, int q, int r)
{
    int n1, n2, i, j, k;
    n1 = (q - p) + 1;
    n2 = r - q;
    int L[n1 + 1];
    int R[n2 + 1];
    for(i = 1 ; i <= n1 ; i++)
    {
        L[i] = A[(p + i) - 1];
    }
    for(j = 1 ; j <= n2 ; j++)
    {
        R[j] = A[q + j];
    }
    L[n1 + 1] = 99999;    // Supposed to be infinity
   R[n2 + 1] = 99999;    // Supposed to be infinity
    i = 1;
    j = 1;
    for(k = p ; k <= r ; k++)
    {
        if(L[i] <= R[j])
        {
        A[k] = L[i];
        i = i + 1;
        }
        else
        {
        A[k] = R[j];
        j = j + 1;
        }
    }
    return 0;
}


Looking forward to your wonderful and helpful replies!
Thanks in advance!
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

A different language to me!

Hi all, I'm glad there's a section called UNIX for dummies on this forum cause when it comes to UNIX, I am certainly a dummie. I honestly don't have a clue about 98% of the UNIX commands out there so forgive me if this is a really stupid question. I'm trying to set up a MySQL server on my Mac... (7 Replies)
Discussion started by: kguenther
7 Replies

2. Programming

c language

is there any difference between "char*" and "char *" (1 Reply)
Discussion started by: amol munde
1 Replies

3. Programming

which language will be the best

Hello, Ive got to amke a script to calculate how many emails are in the servers mailbox, how many times each IP address appears in the mailbox and to search for keywords, tehn produce the results in HTML report. The thing is I dont know where to start and what the best language to use would be. ... (4 Replies)
Discussion started by: clueless
4 Replies

4. UNIX for Dummies Questions & Answers

What language is this?

Hi, Just got handed this script set def = HLA_DR.pockets set data = DRB1_1501.dat foreach a ( 'cat $data | args 3 |sort -u' ) cat $def | xC | grep -v $a > $$.def cat $data | grep $a | args 1,2 > $a.dat set pseudo = `cat MHCDR_pseudo.dat | grep HLA | grep $a | args 2`... (8 Replies)
Discussion started by: lost
8 Replies

5. Shell Programming and Scripting

Representing dir path for copy through env variable

Hello , i am on linux, and im trying to figure out why my cp command cant copy one file. I am trying to use variable in which i stored path to location where is file i wish to copy. $ echo $ORA_ALERT_LOG /u01/app/oracle/diag/rdbms/ring11/ring11/trace $ $ $ pwd /home/oracle $ cp -p... (2 Replies)
Discussion started by: tonijel
2 Replies

6. UNIX for Dummies Questions & Answers

sleep infinity

This might be one of the dumbest questions you've got, but please bear with me: I am a UNIX beginner. I had an test today and I was asked the following question: Q. How do you put the terminal into sleep indefinitely? I didn't know the answer, but after I came home, I tried the following... (9 Replies)
Discussion started by: indyxandy
9 Replies

7. Shell Programming and Scripting

File representing the hard disk storage device

I want example of a file representing the hard disk storage device In UNIX ? (6 Replies)
Discussion started by: tamer11007
6 Replies

8. Shell Programming and Scripting

awk Merging multiple files with symbol representing new file

I just tried following ls *.dat|sort -t"_" -k2n,2|while read f1 && read f2; do awk '{print}' $f1 awk FNR==1'{print $1,$2,$3,$4,$5,"*","*","*" }' OFS="\t" $f2 awk '{print}' $f2 donegot following result 18-Dec-1983 11:45:00 AM 18.692 84.672 0 25.4 24 18-Dec-1983 ... (3 Replies)
Discussion started by: Akshay Hegde
3 Replies

9. Programming

C language help URGENT !!!

I'am writing a program in C language and my code is working perfectly i just need to add a search to it ... My code lets users add companies, and then display them on screen... i would like to add a search that allows user to type company name and then displayall its info on the screen !! THANK... (1 Reply)
Discussion started by: aloushi
1 Replies

10. UNIX for Beginners Questions & Answers

Can't pass a variable representing the output of lsb_release to a docker dontainer

I don't know why, but the rendering of my code mucks up the spacing and indentation, despite being correct in the original file. I'm having issues getting the following script to run (specifically the nested script at the end of the docker command near the end of the script; I think I'm not passing... (2 Replies)
Discussion started by: James Ray
2 Replies
qsort(3C)						   Standard C Library Functions 						 qsort(3C)

NAME
qsort - quick sort SYNOPSIS
#include <stdlib.h> void qsort(void *base, size_t nel, size_t width, int (*compar)(const void *, const void *)); DESCRIPTION
The qsort() function is an implementation of the quick-sort algorithm. It sorts a table of data in place. The contents of the table are sorted in ascending order according to the user-supplied comparison function. The base argument points to the element at the base of the table. The nel argument is the number of elements in the table. The width argument specifies the size of each element in bytes. The compar argument is the name of the comparison function, which is called with two arguments that point to the elements being compared. The function must return an integer less than, equal to, or greater than zero to indicate if the first argument is to be considered less than, equal to, or greater than the second argument. The contents of the table are sorted in ascending order according to the user supplied comparison function. USAGE
The qsort() function safely allows concurrent access by multiple threads to disjoint data, such as overlapping subtrees or tables. EXAMPLES
Example 1 Program sorts. The following program sorts a simple array: #include <stdlib.h> #include <stdio.h> static int intcompare(const void *p1, const void *p2) { int i = *((int *)p1); int j = *((int *)p2); if (i > j) return(1); if (i < j) return (-1); return(0); } int main() { int i; int a[10] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; size_t nelems = sizeof (a) / sizeof (int); qsort((void *)a, nelems, sizeof (int), intcompare); for (i = 0; i < nelems; i++) { (void) printf("%d ", a[i]); } (void) printf(" "); return(0); } ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ |MT-Level |MT-Safe | +-----------------------------+-----------------------------+ SEE ALSO
sort(1), bsearch(3C), lsearch(3C), string(3C), attributes(5), standards(5) NOTES
The comparison function need not compare every byte, so arbitrary data may be contained in the elements in addition to the values being compared. The relative order in the output of two items that compare as equal is unpredictable. SunOS 5.11 6 Dec 2004 qsort(3C)
All times are GMT -4. The time now is 04:47 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy