Representing INFINITY in C language????


 
Thread Tools Search this Thread
Top Forums Programming Representing INFINITY in C language????
# 1  
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!
# 2  
Old 02-18-2012
As you are using signed integers, a "close" approximation of infinity would be the largest representable integer, i.e. 2147483647 with 32-bit int.
# 3  
Old 02-18-2012
There is no representation of infinity in C. jlliagre pointed out the max value for signed integers. There ones for unsigned long int, double, float, etc.

Consider reading the limits.h file in your /usr/include directory. This is where constants are defined, like LONG_MAX, and they work whether you compile either 64 code or 32 bit code, the values simply are declared much larger for 64 bit code.

You should learn about them and use them in your coding.

ps: look into qsort() for sorting.
# 4  
Old 02-18-2012
Quote:
Originally Posted by jim mcnamara
There is no representation of infinity in C.
See ISO/IEC 9899-2011 Section 7.12.4

Quote:
The macro

INFINITY

expands to a constant expression of type float representing positive or unsigned
infinity, if available; else to a positive constant of type float that overflows at translation time.
This User Gave Thanks to fpmurphy For This Post:
# 5  
Old 02-20-2012
fpmurphy - thanks for the correction. Now, If I ever get a modern compiler...
# 6  
Old 02-20-2012
Better wait, even GCC only supports parts of C11!
# 7  
Old 02-22-2012
And don't rule out the obvious:

Code:
$ cat inf.c
#include <stdio.h>

int main(void)  {       printf("%f\n", 1.0/0.0);        return(0);      }

$ gcc inf.c
$ ./a.out
inf

$

of course, that only works for floats. For integers, you're stuck with INT_MAX/LONG_MAX/etc. Since there's no possible integer higher than them for that type, they're as good as infinity for sorting purposes, even if they're not actually infinite.

Last edited by Corona688; 02-22-2012 at 09:01 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. Programming

c language

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

10. 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
Login or Register to Ask a Question