newbie assignment help

This is a discussion on newbie assignment help within the C Programming forums, part of the General Programming Boards category; hey im working on an assignment and am not sure where i went wrong. Code: #include <stdio.h> int main() { ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    10

    newbie assignment help

    hey im working on an assignment and am not sure where i went wrong.
    Code:
    #include <stdio.h>
    int main()
    {
    	int data[99];
    	int val;
    	int i;
    	
    	for(i=0;i<=99;i++)
    	{
    		printf("Enter an integer value (0 to quit): ");
    		scanf("&#37;d", &val);
    		
    		if(val==0 || val==data[99])
    	{
    		printf("Values in reverse order: \n%d\n", data[i]);
    	}
    		
    		
    	}
    	
    	return 0;
    }
    the objective is to accept some numbers and then if the array is full or if "0" is entered, the numbers will print in reverse order (where i encountered problem). also, after it displays "numbers in reverse" it restarts the loop..how can i get out also?

  2. #2
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    Code:
    /* arraylist.c */
    
    #include <stdio.h>
    int main()
    {
    	int data[99];
    	int val;
    	int i;
    	
    	for(i=0;i<=99;i++)
    	{
    		printf("Enter an integer value (0 to quit): ");
    		scanf("%d", &val);
    		
    		if(val == 0 || i>99)
    	{
    	        data[i] = val;	
                    
    
    	}
    		
    		
    	}
    
    
    /* add your reverse printout here */
    	
    	return 0;
    }
    also look in to using while() for your scan loop.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,820
    Well you do have a lot more closing braces than opening braces.

    > for(i=0;i<=99;i++)
    This steps off the end of the array. Use
    for(i=0;i<99;i++)

    Printing in reverse just counts backwards from whatever 'i' you got to.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Think about what you would do on paper. Let's say you have 2, 8, 7, 1, and 6 as your numbers. In reverse order, you would have 6, 1, 7, 8, then 2. Note how the swapping is done. Do the same for your system. One other problem is that you are accessing an array index value (the number in the brackets) outside the allotted range. "number[8] being defined doesn't mean you can access "number[8]" in the code (unless you somehow change the array size, but even then, it's always going to be one less than the value you set as the highest you can use when accessing it). Look into resolving these then look at how to code it properly.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  5. #5
    Linux is where it's at movl0x1's Avatar
    Join Date
    May 2007
    Posts
    72
    Code:
              int arr[20];
              int i;
    
              for (i = 0; i < 20; i++) {
                    scanf("%d", &arr[i]);
    
                    if (arr[i] == 0)
                         break;
              }
    
              if (i == 20)
                  --i;
    
              
              /* reverse */
              do {
                     printf("%d ", arr[i]);
              } while (i-- > 0);
    
              return 0;
    }
    Last edited by movl0x1; 05-14-2007 at 05:08 AM.

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    10
    hmm so i went back and fixed my code and got something like krpytkat's. however, when i get to the second print part, i get stuck and im not really sure why.

    thanks for all the help.
    Code:
    	for(j=data[i];j>=j[0];j--)
    	{
    		printf("The Reverse order is: \n&#37;d", data[j]);
    	}

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    #include <stdio.h>
    int main()
    {
            int MaxNum = 5;
    	int data[MaxNum];
    	int val;
    	int i;
    	
    	for (i=0; i<MaxNum; i++)
    	{
    		printf("Enter an integer value (0 to quit): ");
    		scanf("&#37;d", &val);
    		
                    if (val==0 || i == MaxNum - 1)
                    {
    	            printf("Values in reverse order: \n");
                        for (; i >= 0; i--)	
                            printf("%7d ", data[i]);
                    }
    		if (i < 0) i = 0;
    		if (val == 0) break;
    	}
    	
    	return 0;
    }
    the objective is to accept some numbers and then if the array is full or if "0" is entered, the numbers will print in reverse order (where i encountered problem). also, after it displays "numbers in reverse" it restarts the loop..how can i get out also?
    Your "new" code offered nothing I could make sense of, sorry. I haven't run the code above, so test it out for yourself - it may have simple errors in it, still.

    Anything you don't understand, just let me know.

  8. #8
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    Code:
    	for(j=data[i];j>=j[0];j--)
    	{
    		printf("The Reverse order is: \n%d", data[j]);
    	}
    after you exit the first for() loop your variable is out of scope. Meaning you can not do data[i] in the intializer it has to be
    Code:
        For(j=0 ; j>0 ; I++ )
    edit note should be j++ not i++

    or a variable like this
    Code:
    for (i=0; i<MaxNum; I++)
    if you get stuck write your prog on paper then trace each step and print the output manually. So you know what is going on with your prog. Or write a sutocode like so .... so you know what you want to do....

    Code:
     int main
    
      for() .... get in data with scanf() and store data in array
    
    
     for() .... print array in reverse order
    
    exit or return 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 03:34 AM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 09:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 02:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21