Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

read a variable # of ints from the keyboard into array

Posted on 1998-06-12
6
Medium Priority
?
172 Views
Last Modified: 2011-04-14
I'm looking for a sample program that does exactly this:  gets called from the command line to read in a variable number of integers which get stored into an array.  After whatever manipulation (sorting, adding, multiplying various parts of the array) the array will be displayed back on the screen and the program ends.  Usage should be:
  FeedMe 383 22 82 99 1 93 3 82
for any amount of ints up to 100.  I'd like sample code for FeedMe.cc and FeedMe.h, as well as a sample compile on gcc.
This should be really simple and hopefully someone has it lying around and can post it asap.  Thanks.
0
Comment
Question by:appleby
  • 2
  • 2
  • 2
6 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 1165770
How about this....


0
 
LVL 11

Expert Comment

by:alexo
ID: 1165771
How about:

    #include <iostream.h>
    #include <stdlib.h>


    int Sum(int size, int* array)
    {
        int result = 0;
        for (int i = 0; i < size; ++i)
            result += array[i];

        return result;
    }


    int main(int argc, char* argv[])
    {
        if (argc < 2)
        {
            cout << "Usage: " << argv[0] << " <argument>..." << endl;
            return 1;
        }

        int i;

        int size = argc - 1;
        int* array = new int[size];
        for (i = 0; i < size; ++i)
            array[i] = atoi(argv[i + 1]);

        int result = Sum(size, array);

        cout << "Array: ";
        for (i = 0; i < size; ++i)
            cout << array[i];
        cout << endl;

        cout << "Result: " << result << endl;

        delete[] array;
        return 0;
    }

OK?
0
 
LVL 32

Expert Comment

by:jhance
ID: 1165772
Oops, here's the code:

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

void main(int argc, char *argv[])
{
      // Make a place to store the ints
      if(argc < 2){
            fprintf(stderr, "Error, nothing specified\n");
            return;
      }

      int *Ints = new int[argc-1];
      for(int i=1; i<argc; i++){
            Ints[i-1] = atoi(argv[i]);
            printf("Argument #%d (%s) converted to %d\n", i-1, argv[i], Ints[i-1]);
      }
}
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:appleby
ID: 1165773
I was going to comment that I get segmentation fault, but since I see that both suggestions are the same, it makes me think I have a typo or something.  But I don't see one.  Have you both tested these?
0
 

Author Comment

by:appleby
ID: 1165774
Ok, I was just running an old file before fixing a typo.  Both are fine.  Thanks very much.  Alexo's answer did come through first (even if by mistake) and was more complete with the function example also.  I feel I should give him the grade.

0
 
LVL 11

Accepted Solution

by:
alexo earned 280 total points
ID: 1165775
Great minds think alike :-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question