Question:

Need help with programming?

by  |  earlier

0 LIKES UnLike

1) Create a 1D array a[10] and randomly assign 10 integers to the array

2) Add all the numbers in the array and output the result1

3) Add all the numbers in even index of the array a[0],a[2],....,and minus all the numbers in the odd index of the array a[1], a[3],...., output the result2

4) All the steps should be done in one main function.

 Tags:

   Report

4 ANSWERS


  1. Is this a question... or an answer?

    looks like a statement


  2. Not tested code

    void main()

    {

    int arr[10];

    int result1 = 0, result2=0;

    for(int i=0;i<10;i++)

    {

      arr[i] = (int)rand(100);

      result1 += arr[i];



      if(i%2 == 0) result2 += arr[i];

         else result2 -= arr[i];

    }

    printf ("Result1 = %d, Result2 = %d",result1, result2);

    }

  3. What language is this supposed to be in?

    Here is how to do it in C++:

    #include <iostream>

    using namespace std;

    int main()

    {

    srand((unsigned)time(0));

    int n=0;

    int m=10;

    int x=0;

    int a[10];

    for(x=0,x<10,x++)

    {

    a[x]=(rand()%(m+1-n))+n;

    }

    int t=0;

    for(x=0,x<10,x++)

    {

    t=t+a[x];

    }

    cout<<"result1 is equal to "<<t;

    t=0;

    for(x=0,x<10,x++)

    {

    if(x%2==0)

    {

    t=t+a[x];

    }

    else

    {

    t=t-a[x];

    }

    }

    cout<<"result2 is equal to "<<t;

    cin.get();

    }

    I could also do this in Javascript, but if it's in Basic, Java or Python I wouldn't know how to do it.

  4. Read the chapter in your school book, that the other kids were studying last week. The answers are in there.

Question Stats

Latest activity: earlier.
This question has 4 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.