
C Homework HELP
I need help on a program. Required: Write a program that counts how many elements in an array with N elements are higher than the last element of the array. I need to use functions and the cycle for. I have done this but I can't understand what's wrong with it. Output array ex: 16 45 9 10 > 2 Numbers are higher than the last element.
Code:
# include <stdio.h>
#include <conio.h>
int number (int i, int n)
{
int v[n];
int k=0;
int b=sizeof(v)/sizeof(v[0]);
for (i=0;i<n;i++)
{
if (v[i]>v[b1])
k+=1;
}
return k;
}
int main ()
{
int n,i,k;
printf ("Give N: ");
scanf ("%d", &n);
int v[n];
for (i=0;i<n;i++)
{
printf ("Give the %d element: ", i+1);
scanf ("%d", &v[i]);
}
k=number(i, n);
for (i=0;i<n;i++)
printf ("%4d", v[i]);
printf ("\n");
printf("\n %d numbers are higher than the last element.", k);
getch ();
return 0;
}

June 10th, 2013, 06:54 AM

call the v array in to the function.
Code:
int number(int* v, int n)
{
int i,k=0;
//int b=sizeof(v)/sizeof(v[0]); u know the size, why to calculate again ??
for (i=0;i<n;i++)
{
if (v[i]>v[n1])
k+=1;
}
return k;
}
int main ()
{
int n,i,k;
printf ("Give N: ");
scanf ("%d", &n);
int v[n];
for (i=0;i<n;i++)
{
printf ("Give the %d element: ", i+1);
scanf ("%d", &v[i]);
}
k=number(v,n);
for (i=0;i<n;i++)
printf ("%4d", v[i]);
printf ("\n");
printf("\n %d numbers are higher than the last element.", k);
getch ();
}