LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 02-18-2013, 02:04 AM   #1
Balvinder87
Member
 
Registered: Jun 2012
Location: India
Distribution: debian
Posts: 77
Blog Entries: 1

Rep: Reputation: Disabled
Using Command line arguments


a program that will be executed like this
add 1.c 2.c 3.c
1.c should contain a big number.
2.c should contain another big number
their sum should be in the 3.c(big number may be of 100 digit)
how we can achieve this using command line?
please share your ideas thanks.
 
Old 02-18-2013, 02:42 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,815

Rep: Reputation: 503Reputation: 503Reputation: 503Reputation: 503Reputation: 503Reputation: 503
Foer example:
Code:
#!/bin/sh

NUM1=$(cat "$1")
NUM2=$(cat "$2")
echo "print $NUM1+$NUM2,\"\n\"" | bc >"$3"
 
Old 02-18-2013, 03:42 AM   #3
Balvinder87
Member
 
Registered: Jun 2012
Location: India
Distribution: debian
Posts: 77
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
i want this program in c using command line arguments
my program will read the number from first file and 2nd number from second file and output the result in third file.How can we achieve this
Quote:
#include<stdio.h>
int main() {
int num1[255], num2[255], sum[255];
char s1[255], s2[255];
int l1, l2;

printf("Enter Number1:");
scanf("%s", &s1);
printf("Enter Number2:");
scanf("%s", &s2);

/* convert character to integer*/

for (l1 = 0; s1[l1] != '\0'; l1++)
num1[l1] = s1[l1] - '0';

for (l2 = 0; s2[l2] != '\0'; l2++)
num2[l2] = s2[l2] - '0';

int carry = 0;
int k = 0;
int i = l1 - 1;
int j = l2 - 1;
for (; i >= 0 && j >= 0; i--, j--, k++) {
sum[k] = (num1[i] + num2[j] + carry) % 10;
carry = (num1[i] + num2[j] + carry) / 10;
}
if (l1 > l2) {

while (i >= 0) {
sum[k++] = (num1[i] + carry) % 10;
carry = (num1[i--] + carry) / 10;
}

} else if (l1 < l2) {
while (j >= 0) {
sum[k++] = (num2[j] + carry) % 10;
carry = (num2[j--] + carry) / 10;
}
} else {
if (carry > 0)
sum[k++] = carry;
}


printf("Result:");
for (k--; k >= 0; k--)
printf("%d", sum[k]);

return 0;
}
Here the program uses character array of numbers.I want to do it using files
 
Old 02-18-2013, 04:08 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
You can use open(2) combined with read(2), or fopen(3) combined with fread(3) to read the numbers into your variables. Then use write(2) resp. fwrite(2) to write to the output file.

Also, I think printf("%d", sum[k]) will not print such huge numbers. You will need to write code to convert the integer to a string of digits. Alternatively you could use GNU Multiple Precision Arithmetic Library to do the calculation and the printing.
 
Old 02-18-2013, 04:57 AM   #5
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 808
Blog Entries: 2

Rep: Reputation: 203Reputation: 203Reputation: 203
Quote:
Originally Posted by Balvinder87 View Post
i want this program in c using command line arguments
my program will read the number from first file ...
You should be aware that the filename extension .c is meant for C source code and not numerical input.
 
Old 02-18-2013, 12:27 PM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
The heart of the problem you are trying to solve is the use of very large precision numbers. I suggest doing a Google search for 'arbitrary precision arithmetic'. Your search should probably lead you to 'The GNU Multiple Precision Arithmetic Library', or any number of other such libraries.
--- rod.
 
Old 02-18-2013, 12:59 PM   #7
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,107

Rep: Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114
The subject line of the thread and the OP's second post both indicate the thing the OP needs help with is argc and argv.

I did a google search. The results were disappointing, on topic but very badly written explanations. The first one that didn't look very badly written was:

http://publib.boulder.ibm.com/infoce...ef%2Fmainf.htm

Quote:
Originally Posted by theNbomr View Post
The heart of the problem you are trying to solve is the use of very large precision numbers.
That is the heart of the assignment. But the OP seems to already have a good start at that.

The OP seems to need to know that the command line arguments are processed into the parameters of main() before main is called. With the usual declaration of argc and argv, argv[1] is a pointer to the first command line argument, argv[2] to the second, etc. argc is the number of command line arguments.

Quote:
Originally Posted by Balvinder87 View Post
Here the program uses character array of numbers.I want to do it using files
I don't think that is really what you want (to replace use of char arrays with use of files). I think you want to keep the use of char arrays unchanged.

If is the initial printf and scanf part of your program that you want to replace with getting file names from argv and the opening and reading two files (into the char arrays) then later open and writ the result to the third file.

Last edited by johnsfine; 02-18-2013 at 01:13 PM.
 
Old 02-18-2013, 04:22 PM   #8
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Okay, this seems to replace stdin with FILEs.
(untested)
Code:
FILE * infile;

/* printf("Enter Number1:"); */
infile = fopen( argv[1], "r" );
fscanf(infile, "%s", s1);
fclose( infile );

/* printf("Enter Number2:"); */
infile = fopen( argv[2], "r" );
fscanf(infile, "%s", s2);
fclose( infile );
Error checking on file opening is left as an exercise for the reader.

--- rod.
 
Old 02-20-2013, 12:57 AM   #9
Balvinder87
Member
 
Registered: Jun 2012
Location: India
Distribution: debian
Posts: 77
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
I am using arrays. I don't want you to use arrays to solve this problem.
It should be completely dynamic. User should not enter the size of the number first...he should be able to give the number directly and my program should work.Any one with suggestions,hints.
Thanks
Quote:
#include<stdio.h>
int main() {
int n;
printf("Enter The Size");
scanf("%d",&n);
int num1[n], num2[n], sum[n];
char s1[n], s2[n];
int l1, l2;
printf("Enter The Number 1:");
scanf("%s", s1);
printf("Enter Number2:");
scanf("%s", s2);

/* convert character to integer*/

for (l1 = 0; s1[l1] != '\0'; l1++)
num1[l1] = s1[l1] - '0';

for (l2 = 0; s2[l2] != '\0'; l2++)
num2[l2] = s2[l2] - '0';

int carry = 0;
int k = 0;
int i = l1 - 1;
int j = l2 - 1;
for (; i >= 0 && j >= 0; i--, j--, k++) {
sum[k] = (num1[i] + num2[j] + carry) % 10;
carry = (num1[i] + num2[j] + carry) / 10;
}
if (l1 > l2) {

while (i >= 0) {
sum[k++] = (num1[i] + carry) % 10;
carry = (num1[i--] + carry) / 10;
}

} else if (l1 < l2) {
while (j >= 0) {
sum[k++] = (num2[j] + carry) % 10;
carry = (num2[j--] + carry) / 10;
}
} else {
if (carry > 0)
sum[k++] = carry;
}


printf("Result:");
for (k--; k >= 0; k--)
printf("%d", sum[k]);

return 0;
}

Last edited by Balvinder87; 02-20-2013 at 07:31 AM.
 
Old 02-20-2013, 08:05 AM   #10
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,107

Rep: Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114
Quote:
Originally Posted by Balvinder87 View Post
I don't want you to use arrays to solve this problem.
That is more difficult and messy and I doubt it is what you actually want to do. (But if it is, keep reading because I explain how).

Quote:
It should be completely dynamic. User should not enter the size of the number first...he should be able to give the number directly and my program should work.
But that is easy.

Arrays can be dynamically allocated after you know how big they need to be.

What happened to your earlier request to get the numbers from files and to get the file names from command line arguments? Now you seem to be going in a different direction.

With scanf from STDIN, it is rather hard to know how big the line will be before reading it. So using a dynamic array might require multiple estimates and realloc calls. But if you are reading from a file, it is easy to look ahead and see how big the input will be before storing the input.

If you really don't want to use arrays, you could read digits one at a time constructing a reversed linked list (of individually malloc'ed chunks) as you read the digits. Then it is easy to process the two reverse linked listed (least significant first) while constructing a result list, reversing the links again so the result is forward linked. Then you could display the result by traversing the result list and displaying on digit at a time.

If you have any understanding at all of malloc and pointers, it should be obvious how to construct a linked list that will be read in the opposite sequence that it is constructed. (It is actually a tiny bit easier than making a linked list that will be read in the same sequence it is constructed.)

Last edited by johnsfine; 02-20-2013 at 08:33 AM.
 
Old 02-20-2013, 08:05 AM   #11
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Quote:
Originally Posted by Balvinder87 View Post
I am using arrays. I don't want you to use arrays to solve this problem.
That simply doesn't make sense. The only alternative to arrays in the C language is scalars. To solve this problem with only scalar variables is somewhere near impossible.
If you are simply concerned about the size of the arrays required, you have a few alternatives. You can read data in fixed sizes that are known not to exceed the size of your arrays, and then dynamically resize the arrays as necessary. You can create buffers (arrays) that are arbitrarily large so they will never be overflown. If you are reading from files, you can find their sizes without reading from them, then create arrays sufficiently large to hold the file data. There are possibly other solutions.

--- rod.
 
Old 02-20-2013, 08:31 AM   #12
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,107

Rep: Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114
Quote:
Originally Posted by theNbomr View Post
The only alternative to arrays in the C language is scalars.
C doesn't have the std::stack library class that C++ has. But C was intentionally designed to lack such things while providing the building blocks a programmer needs to make such things.

One simple design is
As you read one digit at a time, push it onto a stack.
The second input number is read one digit at a time and pushed onto a different stack.
Later, as you pop one digit at a time from each input stack, you push the result onto an output stack.
Finally pop one digit at a time from the output stack for display.

All that would be trivial if C had stack objects. If the OP were even a slightly more advanced C beginner, that leads to the simple answer of: first invent a stack data structure and supporting (push/pop) functions in C.

Quote:
If you are simply concerned about the size of the arrays required, you have a few alternatives.
Very true. Using stacks would take much more memory than using arrays.
Maybe the instructor's intent is not teaching how to minimize memory use, but introducing more interesting data structures. I would not guess a directive to not use arrays (if the instructor gave such a directive) is motivated by saving memory.
 
Old 02-21-2013, 01:32 AM   #13
Balvinder87
Member
 
Registered: Jun 2012
Location: India
Distribution: debian
Posts: 77
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
Thanks all for your suggestions.I got the idea and get it done using doubly linked lists.
 
  


Reply

Tags
command line, programing


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Command line arguments ajeesh.tr Programming 1 04-01-2011 07:11 AM
Command Line Arguments sidhuram Programming 4 09-06-2010 04:03 AM
Command line arguments bioinformatics_guy Linux - Newbie 2 02-12-2009 07:26 AM
command line arguments nickraj Programming 6 09-11-2006 01:01 PM
Help with command line arguments ? synapse Linux - Newbie 2 02-23-2004 02:25 AM


All times are GMT -5. The time now is 05:21 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration