Calculating the max element of numbers

Hello, I am a beginner in C language and I have a question.

I have to write a function to calculate the max. elements of numbers, which we put in as input arguments. (to convert the string into a number I have to use atoi function. In the main program we have to check how many arguments there are. If input arguments aren't given, the program says "Result 0", else it prints the max. element.

Here's the code I wrote, but it doesn't work so I ask for help,



#include <stdio.h>
using namespace std;

int najvecjiElement (int argc, char *argv[])
{
int maxStevilo = argv[1];
for (int i = 1; i <= argc; i++) {
if (argv[i] > maxStevilo) {
maxStevilo = argv[i]; }
}
return maxStevilo;
}


int main(int argc, int *argv[])
{
int i;
cout << "Najvecje stevilo:" << endl;
cout << najvecjiElement(argc,argv);

return 0;
}

I don't see where you are using atoi(). Without it, your result will be incorrect.
I changed my code, but it still doesn't work. Any hint?



#include <stdio.h>
#include <stdlib.h>

int najvecjiElement (int para, char *polje[])
{
int atoi(const char *polje[]);
int maxStevilo = polje[1];
for (int i = 1; i < para; i++) {
if (polje[i] > maxStevilo) {
maxStevilo = polje[i]; }
}
return maxStevilo;
}


int main(int argc, char *argv[])
{
if (argv[1] == NULL) {
printf("Rezultat 0.");
}
else {
printf("Najvecje stevilo:\n");
printf("%d", najvecjiElement(argc,argv));
}
return 0;
}
Using code tags and proper indentation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
#include <stdlib.h>

int najvecjiElement (int para, char *polje[])
{
    int atoi(const char *polje[]);
    int maxStevilo = polje[1];
    for (int i = 1; i < para; i++) {
        if (polje[i] > maxStevilo) {
            maxStevilo = polje[i];
        }
    }
    return maxStevilo;
}


int main(int argc, char *argv[])
{
    if (argv[1] == NULL) {
        printf("Rezultat 0.");
    } else {
        printf("Najvecje stevilo:\n");
        printf("%d", najvecjiElement(argc,argv));
    }
    return 0;
}


You cannot set maxStevilo (lines 7 & 10) or compare it to polje[i] (line 9) without using atoi().

Edit: fix typo.
Last edited on
hello,I'm a Chinese. my english weak. I think the code will solve your problem. If you want to calculate the length of char *, replace atoi(*(++argv)) ,use strlen(*(++argv)).
Code:
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
using namespace std;

int maxElementsOfNumber(int argc, char* argv[])
{

int m_maxElement = atoi(*(++argv));
if ( argc == 2)
{
return m_maxElement;
}
for (int i = 1; i <= argc-2; i++)
{
int temp_maxElement = atoi(*(++argv));
if ( temp_maxElement > m_maxElement)
{
m_maxElement = temp_maxElement ;
}
}
return m_maxElement;
}
int main(int argc,char* argv[])
{
if (argc < 2)
{
cout << " The element of number is 0" << endl;
return -1;
}
cout << " The max element of number is: " << maxElementsOfNumber(argc,argv) << endl;
}
Last edited on
Topic archived. No new replies allowed.