# Please, a little help with C

Discussion in 'C' started by Azmodanfury, Oct 29, 2008.

1. ### AzmodanfuryNew Member

Joined:
Oct 29, 2008
Messages:
2
0
Trophy Points:
0
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 = {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 )
{
if( Vector[j+1] > Vector[j] )
numKMagg++;
}

if( Vector[j-1] == Vector )
{
if( Vector[j-1] < Vector[j] )
numKMin++;
}

j++;
}

if( Vector == Vector )
numKMagg++;

if( Vector == Vector )
numKMin++;

printf("%d\n%d\n",numKMagg, numKMin);

return 0;

}```
His works, mine not...

2. ### AzmodanfuryNew Member

Joined:
Oct 29, 2008
Messages:
2
0
Trophy Points:
0
Sorry the string

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

have to be deleted... I don't know how to modify my post Joined:
Jul 12, 2004
Messages:
15,334
377
Trophy Points:
83

4. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,009
203
Trophy Points:
63
Occupation:
Senior Support Engineer
Location:
England
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; int *Vector=&n; then all negative indices of Vector from -1 to -10 would be fine.