Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Please, a little help with C (http://www.go4expert.com/forums/little-help-c-t14857/)

Azmodanfury 29Oct2008 19:39

Please, a little help with C
 
Hello I'm new here and I would like to say that I usually find this forum useful for my C issues but not with the last one...

I'm (trying to) graduate at university of Rome in informatics but I suck in math! I'm used to be a good programmer but not when advanced (for me at least) math is required.

I'm trying to do my best but I really need some help with this problem. I'll paste the exercise to you:

Write a C program that reads 10 integers (positives, negatives or zero) and another one integer (k) and stamps:
1. How many times the k number appears next of one number < of k among the 10 integers (if the first number received is = k that this number must be counted also).
2. How many times the k number appears previous of one number > k among the 10 integers (if the tenth number received is = k that number must be considered also)

Example1:
9, -7, 23, 8, 9, 9, 13, 9, -11, 5, 9
The program must stamps:
2, 1

Example2:
-5, 0, 3, 17, -998, 0, -12, 0, 0, 0, 0
The program must stamps:
3, 2

I'm sorry if the translation may not be correct but I'll try to explain better if needed. I'll paste my program (that obviously doesn't work properly) and next the program made by a friend of mine I used as a little guide.

#include <stdio.h>

int a[11] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int b;
int mins = 0;
int mags = 0;
int i = 1;

Code:

int main()
{
        for( i == 1 ; i < 11 ; i++ ) {
                scanf( "%d" , &a[i] );
        }

        scanf( "%d" , &b );

        i--;

        for( i == 9 ; i > 0 ; i-- ) {
                if( a[i-1] == b ) {
                        mins++;
                        printf("il vettore %d (%d) si mins++\n" , i , a[i]);
                }
        }

        i++;

        for( i == 0 ; i < 11 ; i++ ) {
                if( a[i+1] == b ) {
                        mags++;
                }
        }

        printf( "%d %d" , mins , mags );

        return 0;
}

Code:

#include <stdio.h>

int Vector[ 10 ];
int i=0;
int j=0;
int numKMagg=0;
int numKMin=0;

int main()
{
        while( i<11 )
        {
                scanf("%d", &Vector[i]);
        i++;
        }
       
        while( j<9 )
        {
                if( Vector[j+1] == Vector[10] )
                {
                        if( Vector[j+1] > Vector[j] )
                        numKMagg++;
                }
           
                if( Vector[j-1] == Vector[10] )
                {
                        if( Vector[j-1] < Vector[j] )
                        numKMin++;
                }
               
                j++;
        }

        if( Vector[0] == Vector[10] )
        numKMagg++;
       
    if( Vector[9] == Vector[10] )
    numKMin++;
   
   
    printf("%d\n%d\n",numKMagg, numKMin);
           
        return 0;
       
}

His works, mine not...

Please help me. :undecided

Thanks in advance.

Azmodanfury 29Oct2008 19:43

Re: Please, a little help with C
 
Sorry the string

printf("il vettore %d (%d) si mins++\n" , i , a[i]);

have to be deleted... I don't know how to modify my post :(

shabbir 29Oct2008 22:02

Re: Please, a little help with C
 
The editing option of your post as you get some more posts

xpi0t0s 30Oct2008 14:01

Re: Please, a little help with C
 
I don't understand the explanation. Could you give a few more examples showing where the result numbers come from? At least could you explain where 2 and 1 come from in the first example? And 3,2 in the second?

If you need to count the number of times k appears next to a number less than itself, and the number of times it appears next to a number greater than itself, then given the 10 integers 9, -7, 23, 8, 9, 9, 13, 9, -11, 5 surely the output must be 2 (because the 3rd and 4th 9s are less than 13) and 3 (because 9 is greater than -7,8 and -11), not 2 and 1.

Also example 2 doesn't add up (-5, 0, 3, 17, -998, 0, -12, 0, 0, 0). 0 appears next to -5,-998 and -12 twice, so "less than" is 4 (or 3 if we're only counting the second 0 once; I'm counting it twice because the second 0 is greater than both -998 and -12)), and it appears next to 3, so "greater than" is only 1. So again I don't see where the prescribed output 3,2 comes from.

Your friend's example is not right, by the way. In the j loop, on the first iteration when j==0, it refers to Vector[j-1] which is Vector[-1]. While this will compile and run [because arr[n] is just shorthand for *(arr+n) ], since Vector is not a pointer into the middle of an array but the start of an array, this means the program exhibits undefined behaviour.

If you had something like int n[20]; int *Vector=&n[10]; then all negative indices of Vector from -1 to -10 would be fine.


All times are GMT +5.5. The time now is 14:38.