-3

I was asked to do a program segment on these questions.

  1. That displays the last element of each row contained in Int A[20][50], Use While loop.
  2. That allocates an array D storing the square of integers from 1 to N.
  3. A while loop that determines the product of all odd integers from A to B exclusively.

1

While(a<20){
a++
cout<< A[a][50]<<"\n";
}

edit 1:

#include <iostream>
#include <string>
using namespace std;
int main()
{
int a,n,m;
int  A [20][50]={};
a=1;
cout<<"enter your number:"<<endl;
cin>>n;
cin>>m;
cout <<A [n][m] <<"\n";
    while (a> 20){
         cout << A [a][49]<<"\n";
}
    system ("pause");
    return 0;

edit 2:

 #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    int a,n,m;
    int  A [20][50]={};
    a=1;
    cout<<"enter your number:"<<endl;
    cin>>n;
    cin>>m;
    cout <<A [n][m] <<"\n";
        while (a < 20){

             cout << A [a][49]<<"\n";
            }
             a++;
       }
        system ("pause");
        return 0;
      }

2

float num; float d[num];
 cout<<"How many number do you want to enter to find there squure?"<<endl;
 cin>>num;
 for(int i; i<num; i++){
 cin>>d[i];}
for(int i; i<num; i++) {
result[i]=d[i]*d[i];}
 for(int i=0; i<num i++){
 cout<<result[i];

edit 1

#include <iostream>
#include <string>
#include<iomanip>
using namespace std;
int main()
{
int b;
int x;
int A[]={};
int N;
int n;
   cout <<"enter value for N:"<<endl;
   cin>>N;
x=1;
n=N;
while (x<=N){
   b =x*x;
A[n]=b;
  x++ ;
}
cout <<A[n]<<"\t";
    system ("pause");
    return 0;
}

edit 2

#include <iostream>
#include <string>
using namespace std;
int main()
{
  int x=1,b;
  int N=5;
  while (x<=N){
    b=x*x;
cout<<b<<"\n";
x++;
}
system("pause");
return 0;
}

3

I have no idea on how i can do it :(

Are these codes correct? except number 3 cause I have really have no idea how to do it. can anyone correct these? Thank you very much :)

edit: I made a new answer to number 1 and made compilable :) edit: I edited my number 2 now. But can't seem to get it printed into an array. edit: Was able to make #2 work but I'm sure if It's the output being asked :s

  • 1
    Format your code and your question. Make your code **compilable**. – cadaniluk Jun 04 '16 at 11:30
  • 1
    Code 1 is not correct because `A[a][50]` is dereferencing out-of-range for `Int A[20][50]`. (`Int` is a user-defined type?) – MikeCAT Jun 04 '16 at 11:31
  • Please post a [Minimal, **Complete**, and Verifiable example](http://stackoverflow.com/help/mcve). – MikeCAT Jun 04 '16 at 11:31
  • 1
    Code 2 is not correct because `num` is used before initializing and while its value is indeterminate in `float d[num];` – MikeCAT Jun 04 '16 at 11:31
  • 2
    `While`, which is used in code 1, isn't C++ keyword. – MikeCAT Jun 04 '16 at 11:43
  • @MikeCAT is my code now correct in #1 or should I add an "a++" at the bottom? or anything? – Andrew Lim Jun 04 '16 at 11:56
  • @Downvoter edited it but Im not very sure if what I made is already compilable – Andrew Lim Jun 04 '16 at 11:57
  • 1
    @MikeCAT should that be while instead of While right? – Andrew Lim Jun 04 '16 at 11:58
  • @AndrewLim Yes, it should be `while`. new #1 seems wrong because 1. No elements will be printed because `1 > 20` is false. 2. You should add `a++` somewhere or replace `a` as the index to `a++`. 3. I don't think the first row should be omitted from printing. – MikeCAT Jun 04 '16 at 12:02
  • @MikeCAT okay edited it again. Did I get it correct? :) – Andrew Lim Jun 04 '16 at 12:18
  • No. You should delete `}` before `a++;` – MikeCAT Jun 04 '16 at 12:20
  • @MikeCAT Bro, can you look over at my number 2? I think I got the main operation right but I cant print it in an Array form. How do you do it? – Andrew Lim Jun 04 '16 at 12:54
  • Start with allocating enough buffer based on inputted `N`. At least `N+1` elements are required to use `A[N]`. Note that variable-length array isn't supported in standard C++, so you should use `new[]` operator and `delete[]` operator. By the way, what is "an Array form"? There doesn't seem any specification in the question about printing in #2. – MikeCAT Jun 04 '16 at 12:59
  • @MikeCAT ow I just thought that it should be in that form or something because it says Allocates in Array D. I don't get the question really. But I guess I can answer it if it wasn't in any form of Array. – Andrew Lim Jun 04 '16 at 13:13
  • @AndrewLim : See my alternative [\[ solution \]](http://pastebin.com/raw/uZhsWSka) which uses recursion. Far less superior to the straightforward one but may be used to understand how recursion works. – sjsam Jun 04 '16 at 13:53
  • @sjsam Hi thank you for your alternate solution! It is very good but I still cant uderstand complex coding. :S I really hope to understand it. Thank you :) – Andrew Lim Jun 05 '16 at 07:54

3 Answers3

0

I tried the 3rd number and I got this.

#include <iostream>

using namespace std;

int main(void) {
    cout << "Enter the starting value: ";
    int a;
    cin >> a;

    cout << "Enter the number you wish to stop at: ";
    int b;
    cin >> b;

    // Check if b is less than a.
    if (b < a) {
        cerr << "The stopping value must be greater than the starting value"
             << endl;

        // No need to continue.
        return 1;
    }

    int c = a;  // we start at a.
                // I didn't use a directly because
                // I want to use it to display the result.
    int result;
    while (c <= b) {
        // Check if c is odd.
        if (c%2 == 1)
            result *= c;

        ++c;
    }

    // Display the result.
    cout << "The product of all odd integers from "
         << a
         << " to "
         << b
         << " is "
         << result
         << endl;

    // Keep window open.
    cin.get();
}
  • the assignments asks for "the product of all odd integers from A to B **exclusively**" but you include A and B – 463035818_is_not_an_ai Jun 04 '16 at 12:12
  • Please, note that the condition `c%2 == 1` can check only [positive odd numbers](https://ideone.com/qNItpg), you should properly initialize `result` and that you don't really need to check every number (but you should sikp the extremes as for the assignment): `int result = 1; c += c & 1 ? 2 : 1; while ( c < b ) { result *= c; c +=2; }`. – Bob__ Jun 04 '16 at 14:35
  • @Bob__ Bro can where do I place this code? what parts do I have to replace? sorry for the late reply. Thank you :) – Andrew Lim Jun 05 '16 at 07:52
  • @AndrewLim the snippet in my comment should replace the part of Robert's code starting from `int result;'... – Bob__ Jun 05 '16 at 07:57
  • @Bob__ Ow thank you very much :) Just 1 more question tho. I cant understand this part c += c & 1 ? 2 : 1; – Andrew Lim Jun 05 '16 at 14:31
  • @AndrewLim sorry, I used the [conditional operator](http://stackoverflow.com/questions/392932/how-do-i-use-the-conditional-operator) instead of writing `if ( c & 1 ) c += 2; else ++c;`. The assignment asks to skip the extremes, so if c is odd, add 2 to start from the next odd number, else if it's even, add only 1. – Bob__ Jun 05 '16 at 15:20
0

Here is my attempt for number 2.
P.S. You can make this code even more elegant by abstracting some parts in a function.

#include <iostream>

using namespace std;

int main(void) {
    cout << "Please enter the number of integers whose squares you wish to "
         << "compute: ";

    // Read the number of integers.
    int n;
    cin >> n;

    // Declare an array to hold the squares.
    // You can call it a variable length array if you want.
    int squares[n];

    // Invariant: we have squared n integers.
    for (int i = 0; i != n; ++i) {

        // square (i+1) and add it to the array.
        squares[i] = (i+1)*(i+1);
    }

    // Display the result.
    cout << "Here are the squares of integers from "
         << 1 << " to " << n << endl;

    // Invariant: we have printed n squares to std::cout.
    for (int i = 0; i != n; ++i)
         cout << i+1 << ": " << squares[i] << endl;

    return 0;
}
  • Thank you for your kindness bro! :) You answered all of my questions! Than you for your time and effort. May God bless you bro :) – Andrew Lim Jun 06 '16 at 12:01
  • You are welcome my brother, feel free to practice with, and modify them. Try breaking things and see what happens, it's a good way to learn. Just don't be frustrated when you get errors, read the error messages. –  Jun 07 '16 at 04:45
0

And here is what I got for the first question.

#include <iostream>

using namespace std;

int main(void) {
    // Decalare an array of 20 rows,
    // each containing an array of 50 integers.
    int A[20][50];

    // Initialize all the elements of the array to 0.
    // Invariant: we have initialized `row` rows.
    for (int row = 0; row != 20; ++row) {

        // Invariant: we have initialized `col` columns.
        for (int col = 0; col != 50; ++col)
             A[row][col] = 0;
    }

    // We can now fill in the values we want into the array.
    // For this example, we are filling the 1st row with 0,
    // 2nd row with 1, 3rd row with 2 and so on.
    // You can fill it with any values you like.
    for (int row = 0; row != 20; ++row) {
        for (int col = 0; col != 50; ++col)
            A[row][col] = row+1;
    }

    // Display the last element of each row.
    // Invariant: we have printed `row` rows.
    for (int row = 0; row != 20; ++row) {

        // The index -1 gives us the last element in an array.
        cout << "A[" << row << "][50]: " << A[row][-1] << endl;
    }

    return 0;
}