Question

So im having a minor problem with this. I just dont know if im doing it correctly. The question is vague. (to me) Was wondering if I could get some help because ive been at this simple problem in my book for 2 hours now and its ticking me off! Thanks in advance :)

"Write a program that populates "fills" an array of 100 integer elements with the numbers from 1 to 100 and then outputs the numbers in the array."

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
const int size = 301;
int N, I, k;
int score[size];
srand(time(0));


cout   << setprecision(2)
       << setiosflags(ios::fixed)
       << setiosflags(ios::showpoint);

//1)Get # of bowlers ..............................................................
      cout << "Enter number of bowlers? (Must be between 1 and 301) ";
      cin >> N;
      while (N<1||N>size)
{
      cout << "\nError!! Must be between 1 and 301!! ";
      cin >> N;
}
//2) and 5)  Get scores ............................................................
for(I = 0; I<N; I++)
{
//cout << "\nEnter score for bowler # " << I + 1 << " ";
//cin >> score[I];
score[I]=rand()%301;

for(k=0; k<I; k++)
{
    if(score[k]==score[I])
    {
        I--;
        break;
    }

}
}

//3)Get Sum/Avg .....................................................................
int sum = 0;
float avg;
for(I = 0; I<N; I++)
{
sum += score [I];

}

avg = float(sum)/N;




//4) Output scores, sum, and avg ....................................................

for(I = 0; I<N; I++)
{
cout << "\nScore for bowler # " << I + 1 << " is  "  << score[I];

}
cout<<"\n\n The Sum is " << sum << "\n";
cout <<"\n The Average is "<< avg << "\n";


cout<<"\n\n\n";
system ("pause");
return 0;
}
Was it helpful?

Solution

The core of the code just needs to create the array, e.g. with

int arr[] = new int[100];

and then fill it in a for loop, e.g. with

for (i = 0; i<100; i++) arr[i] = i+1;

Note that array indices count from zero, but you want to fill starting from one.

OTHER TIPS

Are you sure that the code has something to do with your problem?

A sample program that does what you want is this:

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

#define N 100

int main(void)
{
        int arr[N], i;

        for (i = 0; i < N; i++)
                arr[i] = i + 1;

        for (i = 0; i < N; i++)
                printf("%d ", arr[i]);

        return 0;
}  
#include <iostream>
#define NUM_VALUES 100

int main()
{
    int i = 1;
    int array[NUM_VALUES];

    while (i <= NUM_VALUES)
    {
        array[i-1] = i;
        i++;
    }

    i = 1;

    while (i <= NUM_VALUES)
    {
        std::cout << array[i-1] << " ";
        i++;
    }

    std::cout << std::endl;
    return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top