Thread: Array of Intergers

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    99

    Array of Intergers

    Im having a problem with this program in which I want to test if a function (math function) is a function by a set of ordered pairs. To accomplish this I need to place the x and y values into separate arrays.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #define MAX 20
    #define ORP MAX/2
    
    int main()
    {
        char pair[MAX];
        int i, l, k;
        int x[ORP], y[ORP];
        printf("Enter a set of ordered pairs: ");
        fgets( pair, sizeof(pair), stdin);
        
        atoi(pair);
        for (i=0 ; i != '\0'; i++)
        {
            if ( ispunct(i) != 0 )
            { 
            x[i]=pair[i-1];
            y[i]=pair[i+1];
            }
            else
            {
                printf("You need commas to enter ordered pairs\n");
                getch();
                return 1;
            }
        }
    
    //------------CUT HERE--------------//
    I get the problem after the if statement testing for punctuation.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    atoi(pair);
    This line does absolutely nothing. Personally, I'd just use something like strchr to search the array for the ',' character. If it returns NULL, there is no ',' in the array, and your code is invalid. Or you could use sscanf to scan the two values into their respective arrays. Just be sure and check the return value of said function.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    99
    I personally would like to keep the same ideas through the code but I want to know why it will not work. I don't see a problem with the expression in the if statement. The problem is in the fact that the arrays x and y are not having the appropriate values placed in them.
    Last edited by _Cl0wn_; 02-19-2003 at 06:17 PM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This is wrong:

    for (i=0 ; i != '\0'; i++)

    The loop never executes. 0 is the same thing as null. You assign 0 to your value, then test that same value for zero.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Also, why do you feel it necissary to create 2 arrays? It would make much more logical sense to create a single array of structures -- each with both an x and y members. An ordered pair is a single concept so it makes logic to store it in a single structure.

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    99
    Well I want two arrays to print the range and domain of the function later in the program.

    Updated full code.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    #define MAX 100
    #define ORP MAX/2
    
    int main()
    {
        char pair[MAX], *pos;
        int i=0, l, k, len=0, temp, tmp;
        int x[ORP], y[ORP];
        
        printf("Enter a string of ordered pairs: ");
        fgets( pair, sizeof(pair), stdin);
        len = strlen(pair);
        
        for ( i=0; i < len; i++)
        {
            if (ispunct(pair[i]) == 0)
            {
                 printf("Ordered pairs must contain commas in this format \"x,y\"\n");
                 getch();
                 return 0;
            }
            else
            {
                 temp=pair[i-1]; x[i]=temp;
                 tmp=pair[i+1]; y[i]=tmp;
            }
            
        }
        for ( l=0; l<len-1; l++)
        {
            for (k=1; k<len-1; k++)
            {
                 if (x[i] == x[i-1])
                 {
                      printf("Does not define a function\n");
                      getch();
                      return 0;
                 }
            }
        }
        printf("Defines a function\n");
        printf("Domain: ");
        for ( i=0; i < len; i++)
            printf("%d, ",x[i]);
        
        printf("Range: ");
        for ( i=0; i < len; i++)
            printf("%d, ", y[i]);
        
        getch();
        return 0;
        }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That code is just wrong. Your input line is just nasty. I mean, sure, code wise it's fine, but logicly, it's crap.

    You never exactly tell them what to enter, you just give them an error when they enter the wrong thing. You should be doing something like:

    "Enter pairs of numbers, one pair at a time, or Q to quit. Example: 1,2"

    Then, you loop through, accepting input until they enter Q.

    Otherwise, if you have them do it the current way, you'll end up with:

    1,2 3,4 5,6 7,8 ...

    Which is much more of a pain in the ass to parse, to make sure they haven't messed up and done something like:

    1,2,3,4 ...

    And again, you never actually say exactly what they are supposed to enter. You never say "Enter two numbers, seperated by a coma." You just say to enter pairs of numbers.

    One of the keys of programming anything relating to user input or user interaction is to treat everyone like they don't know anything, and to expect them to intentionally try to break your code.

    If you adhear to the above, you'll end up with well described actions, that don't break.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    99
    Ok....that is nice, I don't plan to turn this in for an assignment. I don't plan to give this to anyone without an idea for it is used. This is for my benefit to learn based on a challenge on another board not for user readibility. Changing the outout is not going to fix the run-time errors, so again let me ask what is wrong with the code. What logically am I doing in a crappy manner quzah or anyone who knows but quzah seems to know?

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The point wasn't to insult you. The point was for you to read your code yourself and see a much better way to do it. That's the whole point of programming. Or maybe not, code however you feel. Just don't be so opposed to rethinking your view on existing code. I mean seriously, that's the entire point of posting here; to get another view on what you're doing.

    But, hey, what do I know, right?

    I have already dold you your problem. Your problem is that your loop just plain sucks. It is not a good way to validate user input. You however don't like my explanation of why it doesn't work, even when it is obviously logicly flawed. IE: Your validation is not working right because it is wrong. That will not validate user input. Furthermore, you never have said what you expect a line of input to be.

    Without going on what the input is supposed to look like, how am I supposed to tell you why it isn't working? I have no idea what to expect for user input. You apparently want pairs, seperated by a comma, but there are to be multiple pairs on a single line of input ... seperated by what exactly?

    Additionally, you don't allow for digits larger than 1 (ie 0 - 9 ) in your checks, but again, you don't specificly say that anything outside of the range of 0-9 is unacceptable. How in the hell am I supposed to know what is valid if you won't tell me? Furthermore, how am I supposed to fix your code if you won't tell me what is valid?

    Don't come crying to me when it's broken, when you ignore my explanation of why it is broken, and you do nothing to explain to us what exactly the program is supposed to do.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Jan 2003
    Posts
    99
    The program takes a string of ordered pairs (x,y). The pairs are separated by a space and the x and y coordinates by a comma. The loop is there to check each variable in the array against every other variable to make sure that there are no repeated values thus defining false function (Alg function, not programming function). I thought that since this was the basic idea of a bubble sort then I would use that but I suppose that is wrong. Maybe I am a bad programmer because I refuse to revamp this thing or a good one because I am trying to over come this challenge, doesn't matter to me. Also I did change the one loop you were talking about earlier:
    Posted by Quzah
    This is wrong:

    for (i=0 ; i != '\0'; i++)

    The loop never executes. 0 is the same thing as null. You assign 0 to your value, then test that same value for zero
    So I did listen somewhat

    Does this posting suffice for you?
    EDIT:
    Sample input would be something along the line of
    2,4 4,6 2,7 5,7

    Output for this one would be
    Does not define a function

    2,5 6,3 8,1 7,3

    Output for this one would be
    Does define a function
    Domain: {2,6,8,7}
    Range: {5,3,1,3,}

    Edit2: Didn't want to make another reply for this but are there any functions that will take in a string of integers and parse the string with every space encountered? I thought about that on my way back from Arby's but more than likely that is my major problem with this code. For instance I want a string of 5 or 6 grades. Will a function like fgets or scanf grab the string and assign each grade to its own index and not place the spaces in an index?
    Last edited by _Cl0wn_; 02-20-2003 at 03:55 PM.

  11. #11
    Registered User
    Join Date
    Jan 2003
    Posts
    99
    Well I did get it to work. Thanks quzah for the help and here is the final if anyone cares.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main()
    {
    	 int xarray[20], yarray[20];
        int x, len, i=0;
        
        printf("How many coordinates will you enter(Less than 20): ");
        scanf("%d", &len);
        
        for ( x=0; x < len; x++)
        {
        	printf("Enter your coordinates \"x y\"\n");
        	scanf("%d %d",&xarray[x], &yarray[x]);
        }
        
        for ( x=len-1; x>0; x--)
        {
        	for ( i=0; i < x; i++)
        	{
        		if ( xarray[i]>xarray[i+1] )
        		{
        			int tmp;
        			tmp=xarray[i];
        			xarray[i]=xarray[i+1];
        			xarray[i+1]=tmp;
       		}
      		}
       }
       
       for ( x=0; x < len; x++)
       {
       	if ( xarray[x] == xarray[x+1] )
       	{
       		printf("Does not define a function\n");
       		getch();
       		return 1;
      		}
       }
       printf("These coordinates do define a function\n");
       printf("Domain: ");
       for (x=0; x < len; x++)
            printf("%d ",xarray[x]);
       
       printf("Range: ");
       for (x=0; x < len; x++)
       	printf("%d ",yarray[x]);
      	getch();
       return 0;
       }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM