Arrays

This is a discussion on Arrays within the C Programming forums, part of the General Programming Boards category; Your help would be much appreciated I need to write a pseudocode (algorithm) for the following program definition: Write a ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    10

    Help with arrays

    Your help would be much appreciated

    I need to write a pseudocode (algorithm) for the following program definition:
    Write a program to read an integer count (between 1 and 10) and then read count integer values to put into the array. For each value, call the inveriting function and put the inverting value into the second array of doubles. Then print out a report in two lines and count +1 columns to display the original and the inverted values and the largest value of each array in squared brackets.
    Last edited by SARAHCPS; 10-29-2005 at 09:34 AM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    What part are you having trouble with? Note that you won't get much help if it looks like you're trying to get us to do it for you.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    10

    This is what I have...I'm not trying to get you to do it....

    Code:
    #include <stdio.h>
    
    int
    main(void)
    {
    	int i;
    	 double a[];
    
            printf ("Enter ten interger values:");
    		scanf ("%d", a);
    		printf("\nOriginal Values: %d",&i[10]);
    
    		for (int i = 0; i < 10; i++)
       {
            a[i] = 1 / i;
    		printf("\nInversed Values: %d",&a[i]);
       }
    
    
    return 0;
    
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Don't you listen to your compiler? Your array 'a' has no size. Also, 'i' isn't an array, so you can't do what you're doing with it here:
    Code:
    printf("\nOriginal Values: %d",&i[10]);
    Even if it was, you haven't actually put any values into it (which you can't, because it's not an array, but if you did...), so there's nothing to display there.
    Code:
    prompt for number to read, then read it into some variable
    for counter = 0; counter < number you just read; counter++
        read a number into array[ counter ]
    
    ...do whatever else you are doing once you're done reading them in...
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396

    Wink

    You have some indentation issues. Doesn't this look better:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int i;
        double a[];
        
        printf ("Enter ten interger values:");
        scanf ("%d", a);
        printf("\nOriginal Values: %d",&i[10]);
    
        for (int i = 0; i < 10; i++)
       {
             a[i] = 1 / i;
             printf("\nInversed Values: %d",&a[i]);
       }
       return 0;
    }
    Couple of questions:
    1. Where is the pseudocode, I see actual code, with some errors .
    2. Now, where in your program do you get input 10 times?
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    10

    I have tried runing the program; 1 error

    When I run this program below I get this error
    c20 file does not end in a new line



    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int i;
        double a[11], counter;
        
        printf ("Enter ten interger values:");
        scanf ("%d", a[11]);
        printf("\nOriginal Values: %d",a[11]);
    
    	for (counter = 0; counter <=10; counter++)
    
        for (int i = 0; i < 10; i++)
       {
             a[i] = 1 / i;
             printf("Inversed Values: %d",&a[i]);
       }
       return 0;
    }

  7. #7
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Just a wild guess here, but I expect the reason you get the error "file does not end in a new line" is because the file does not end in a new line. Make sure you hit enter after the final }

    By the way, there are many other problems with your program:

    - integer is spelled integer.
    - Why are you trying to store integer values in an array of doubles?
    - scanf can't magically put 10 integers into an array, you'd need to call scanf 10 times, or put 10 %d's in the format specifer
    - ditto with regard to printf, use a loop.
    - in your final printf, you don't need the &, but now I see why you need a double type, so you need to modify your initial scanf to write in doubles. Again, use a loop for each of the 10 values.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    And this line
    Code:
    a[i] = 1 / i;
    should have a double involved, like this:
    Code:
    a[i] = (double)1 / i;
    Otherwise the end number is truncated.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >a[i] = (double)1 / i;
    What a waste of a cast. How about this instead:
    Code:
    a[i] = 1.0 / i;
    My best code is written with the delete key.

  10. #10
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Yes, except the OP more likely meant to use a[i] = 1 / a[i], in which case you would already have a double in the expression, since I suggested changing it to a double.

    From the original post, they want to show the inverted values of the numbers entered, not the reciprocal of the numbers 1-9.

    There are a few other minor errors, but I wanted the OP to think for themselves about the solution. For example, two arrays would be required, one for the original values, and one for the inverse, unless they just printed them on the fly.
    Last edited by cwr; 10-29-2005 at 10:35 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

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