Thread: Problems with 2 assignments

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    4

    Problems with 2 assignments

    Hi guys, I really need your help with these assignments since I can't figure them out.


    http://b.imagehost.org/0320/Capture1.jpg


    Sorry if I made translation mistakes.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    1) Read the homework policy on this site
    2) If you do need help with homework you have to provide your best effort (preferably in code) as well as the impression that you understand the problem domain in general as well as where you are stumped in your code.

    Most people here have already been through school and feel no need to do somebody else's homework. They will help along though if you prove that you are taking it seriously and are responsible about getting it done.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by pauzza View Post
    Hi guys, I really need your help with these assignments since I can't figure them out.


    http://b.imagehost.org/0320/Capture1.jpg


    Sorry if I made translation mistakes.
    Please not that this forum (and every other forum I've seen) had a strict policy of not doing homework for people. It doesn't help you learn anything anyway.

    We will, however, help you out with specific aspects of problems. What exactly are you having problems with? Do you understand the questions? What work have you done so far?

    The more specific you are (and the more effort to tackle the problems yourself you show) the more likely you are to get helpful answers.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    And he is gone.... So depressing that people just expect other people to do everything for them nowadays.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    Yeah you're both right. Then maybe you could tell me whats wrong with this program

    Code:
    #include <stdio.h>
    int main()
    {
     int array[100], n, i, much;
    
        printf("enter length of array  \n");
        scanf("%d", &n);
    
            for (i=1;i<=n;i++)
              {   printf("enter a number for %d \n", i);
                  scanf("%d", &array[i]);
               }
    
            for(i=1;i<=n;i++)
               {
                 if ((array[i] > 0) && (array[i+1] > 0))
                    {much++;}
               }
    
            printf("%d", much);
    
        return 0;
    
    }

    thats the first assignment.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Arrays are indexed starting from zero. So in your program, since your loop goes from 1 to n inclusive, you need to index it like this: array[i - 1].
    Better to loop like this: for (i = 0; i < n; i++) and use array[i]. That's more common.

    much needs to be initialized to zero. You can do that at the place you define it.

    In the second loop you are indexing too far.

  7. #7
    Novice
    Join Date
    Jul 2009
    Posts
    568
    I believe that the first assignment ask for a dynamically allocated array, the length of which depends on input for n. You are assuming that there will be no more then 100 integers. If you haven't learned about dynamically allocated memory yet, you can ignore this remark.

    Other then that:
    (1) Indexes for an array of N elements run from 0 to N-1, inclusive.
    (2) much is not initilized before first use, this will produce undefined results.
    (3) You should be careful with expressions like array[i+1], you will go out of bounds eventually.

    EDIT: Bah. Beaten.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    Thanks with this that's one down

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignments or challenges
    By Dogmasur in forum C Programming
    Replies: 12
    Last Post: 08-20-2008, 02:24 AM
  2. Replies: 3
    Last Post: 04-06-2007, 05:10 PM
  3. C++ programming assignments and tests
    By NeoNite in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2003, 05:41 AM
  4. Common Problems
    By WolfPack in forum C Programming
    Replies: 4
    Last Post: 01-28-2003, 06:38 PM
  5. Coding Problems
    By RpiMatty in forum C++ Programming
    Replies: 12
    Last Post: 01-06-2002, 02:47 AM