LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-30-2023, 01:35 AM   #16
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,080

Rep: Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364Reputation: 7364

Quote:
Originally Posted by NevemTeve View Post
https://en.m.wikipedia.org/wiki/Comma_operator
Counting the commas it slow and not universal solution. How about using an array?
Code:
{
    long long arr[] = { values };
    size_t narr= (sizeof arr)/(sizeof arr[0]);
    maxval= maxfun (narr, arr);
}
That's what I already told, we can use the original argv array, do not need to do anything with it.
Code:
int check_argument(char *s) {
   returns int if can be converted to int
   returns INT_MIN in case of any problem
}
int max_val=INT_MIN
for (int i=0; i<argc; i++) {
    val=check_argument(argv[i])
    if (val>max_val) {
      max_val=val
    }
}
return max_val
This is just a non-working skeleton or just an idea to work with.
 
1 members found this post helpful.
Old 04-30-2023, 06:55 AM   #17
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,883
Blog Entries: 1

Rep: Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872
As a macro:
Code:
#define LLMAX(into,...) \
do { \
    long long arr[] = { __VA_ARGS__ }; \
    size_t narr= (sizeof arr)/(sizeof arr[0]); \
    maxval= llmaxfun (narr, arr); \
} while(0)
 
Old 04-30-2023, 06:57 AM   #18
Racho
Member
 
Registered: Oct 2021
Posts: 59

Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
https://en.m.wikipedia.org/wiki/Comma_operator
Counting the commas it slow and not universal solution. How about using an array?
Code:
{
    long long arr[] = { values };
    size_t narr= (sizeof arr)/(sizeof arr[0]);
    maxval= maxfun (narr, arr);
}
Yup, using an array is the way. And it lets casting the arguments!

I have this piece of code that meets the initial requirements:
- It has to work with long long int
- Don't tell the function how many arguments you are passing to it

And now I think it works fine:
Code:
#include <stdio.h>
#include <limits.h>

long long int llin_max(int num_args, long long int* args) {
	int i=0;
	long long int max, new_num;
	max = LLONG_MIN;
	for (i=0; i < num_args; i++) {
		max = max > args[i]? max : args[i];
	}

	return max;
}

#define countparams(...)  sizeof( (long long int[]) { __VA_ARGS__}  ) / sizeof(long long int)
#define lli_max(...) llin_max(countparams(__VA_ARGS__), (long long int[]) {__VA_ARGS__})

int main() {
	long long int num=0;
	long long int a = 5000, b = 77500, c = -583;
	int inta = 5000, intb = 977500, intc = -583;

	num = lli_max(a, b, c);	
	printf("The greatest number is: %lli\n", num);

	num = lli_max(inta, intb, intc); 
	printf("The greatest number is: %lli\n", num);
	
	num = lli_max((5,6),(7,11),2,(50,3));	// Now this works
	printf("The greatest number is: %lli\n", num);

	return 0;
}

Last edited by Racho; 04-30-2023 at 07:05 AM. Reason: Retiring a comment and a printf with debugging purpouses
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting output of one function into array and feed value into variable in other function in ruby Sivagurunathan Programming 3 11-23-2021 09:38 AM
[SOLVED] Threaded function cannot call a function with extern "C" but nonthreaded function can morty346 Programming 16 01-12-2010 05:00 PM
AWK a variable Ouptut to a new variable and using the new variable with the old one alertroshannow Linux - Newbie 4 02-16-2009 12:08 AM
function showing a list of variable and value: (dynamic variable name) budhax Linux - Newbie 1 09-19-2008 07:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration