Array input/output problems?

Go To StackoverFlow.com

-4

I am trying to learn C++ and I am having problem writing a simple program. What I want is a function that will take one integer input parameter, create a sequence of numbers stored in an array from 0 to that number, and the numbers are a summation. For example, given 7 outputs 0 1 2 3 4 5 6 7

2012-04-03 22:09
by user1311517
What have you done so far, and what are you having problems with - Jerry Coffin 2012-04-03 22:11
You just edited your question, can you please post your code - Jared Burrows 2012-04-03 23:55
What have you tried?Some programmer dude 2012-04-04 06:10


2

You said you would like to fill an array where you plug in a value such as "7" and the array will fill up from 0 to 7.

This can easily be done:

#include <stdio.h>
#include <malloc.h>

int main() {

int i = 0, num = 0; //declare variables
scanf("%d", &num);
int *myArray = (int *)malloc(sizeof(int)*(num+1)); //malloc for array

for (i = 0; i <= num; i++){
    myArray[i] = i;  //fill array as you asked
    printf("%d", myArray[i]);   //print out tested values: 01234567
}

free(myArray);
return 0;
}
2012-04-03 22:27
by Jared Burrows
this works great but is there any way to have an input method for each variable instead of manually changing the code - user1311517 2012-04-03 23:05
This doesn't work great, it's buffer overrun central - in particular, you're writing to myArray[7], which would be the 8th element of the 7-element array myArray. Also, why include iostream and then use printf - Stuart Golodetz 2012-04-03 23:27
Well do you have any example of what to do? I really just need an example code so I can work from there - user1311517 2012-04-03 23:34
Guys, this is just a quick example, we can always make this more robust. I'll edit my answer for your input : - Jared Burrows 2012-04-04 00:58
@user1311517: Between mine and Jacob's answers, you pretty much have what you need (really!). I would go back and look again - Stuart Golodetz 2012-04-04 07:51
@MasterJB: Fair enough (although reverting to C-style code was not the change I had in mind :)). Bear in mind though that there's a difference between robustness and correctness - I was objecting because your code wasn't correct - Stuart Golodetz 2012-04-04 07:56
O I see now, thanks I was doing multiple things. I just wanted to help out - Jared Burrows 2012-04-04 09:00


1

C-style:

#include <stdio.h>
#include <malloc.h>
int main()
{
     int num;
     scanf("%d", &num);
     int *arr = (int *)malloc(sizeof(int)*(num+1));
     int i;
     for(i = 0; i <= num; i++)
         arr[i] = i; //This is the array
     return 0;
}

C++ style:

 #include <vector>
 #include <iostream>
 using namespace std;
 int main(int argc, char ** argv)
 {
      int num;
      cin >> num;
      vector<int> arr;
      for(int i = 0; i <= num; i++)
           arr.push_back(i);
      return 0;
 }
2012-04-03 22:15
by Jacob


0

By way of giving you a helping hand, start here and fill in the blanks:

#include <vector>

std::vector<int> make_sequence(int last)
{
    std::vector<int> result;
    // <fill this in>
    return result;
}

int main()
{
    // <probably do something useful here too...>
    return 0;
}

You're going to have to do some of this yourself though, that's the way StackOverflow works with regard to homework-like problems :)

2012-04-03 22:12
by Stuart Golodetz
Ads