Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
June 9th, 2013, 06:40 AM
 keivi
Registered User

Join Date: Jun 2013
Posts: 1
Time spent in forums: 23 m 39 sec
Reputation Power: 0
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[b-1])
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;
}```

#2
June 9th, 2013, 07:12 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,266
Time spent in forums: 2 Months 4 Weeks 1 Day 19 h 11 m 22 sec
Reputation Power: 1827
cross-posted here
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

#3
June 10th, 2013, 06:54 AM
Registered User

Join Date: May 2013
Posts: 15
Time spent in forums: 5 h 34 m 18 sec
Reputation Power: 0
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[n-1])
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 ();
}
```

 Viewing: Dev Shed Forums > Programming Languages > C Programming > C Homework HELP