FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Arrays in C





ChrisCh
Hey everyone Smile I'm trying to learn to write a program that prints the highest number on an array list. Here's what I've done so far:
Code:
#include<stdio.h>
#include<stdlib.h>

 int maxNumbers(int[], int);

main() {
    /* Load an integer array with values */
   int numbers[20] = {10, 34, 23, 54, 23, 12, 56, 76, 34, 21, 25, 46, 67, 69, 53, 28, 84, 90, 92, 12};

   printf("The maximum number in the list is %d.\n", maxNumbers(numbers, 20));
   /* Pause to see output */
   system("pause");
}
//THIS IS WHERE I'M HAVING PROBLEMS:
 int maxNumbers(int list[], int count) {
   int max = 0, x;

   for(


Does anyone know how to do this? I'm strugling with this - we have to know the basics like this in order to do an assignment later in the term! If anyone knows anything, I would greatly appreciate your help Smile
Liu
Code:

int maxNumbers(int list[], int count) {
  int max=0;
  int x;
  for (x=0; x<count; x++){
     if(list[x] > max)
       max = list[x];
  }
  return max;
}
Traveller
Liu wrote:
Code:

int maxNumbers(int list[], int count) {
  int max=0;
  int x;
  for (x=0; x<count; x++){
     if(list[x] > max)
       max = numbers[x];
  }
  return max;
}


Inconsistent variable usage. Should be:

max = list[x];
Liu
Oops, forgot to correct that, thank you.
ChrisCh
thanks guys, that helped a lot Very Happy
deepak
Travler has given you a decent solution but if you can work with C++ then you could use STL.

Code:

#include<vector>
#include<algorithm>
using namespace std;
int main()
{
     vector<int> a = {1, 3, 4, 5, 6, 7, 11, 1, 3}
     return *max_element(a.begin(), a.end());
}
Related topics
Command and Conquer
PHP Write to line
Funny C variable
[php scripts ] phpweather&email
What can I do with C-Panel?
HELP ME in C++
Languages in c++
C++ TUTORIAL : INTRODUCTION TO C++
Visual Basic Tutorial - Control Arrays: What, Why and How
C++ Project
Dynamic arrangement of buttons in C# Win Form
Programming in C problem
[C] Splitting names program
PHP page from arrays displays nothing, what's going on?
Where should I start? C++ vs. JAVA
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.