HW assignment

This project is too simple but I am having a hard time understanding the question. What does he mean by "named Array type"?


// Write a declaration of a named array type, and then
// declare two arrays of that type. The array type should
// be called "DataSet", and the two two arrays should be called
// "input" and "output". Each array should hold five float values.
// Array "input" should be initialized in the declaration to
// contain 1.0, 2.0, 3.0, 4.0, and 5.0. Array "output" should be
// initialized by a For-loop to contain 2.0, 4.0, 6.0, 8.0, and 10.0
// (think about how to do that!).
//
// Write another For-loop that adds corresponding elements of two
// arrays and prints out the result.


THIS IS WHAT I HAVE TRIED FOR THE FIRST PART OF THE QUESTION. THE PROGRAM WILL NOT GIVE ME THE PROPER VALUES FOR THE OUTPUT, IN FACT IT DOES NOT COMPILE.

#include

using name space std;

int main()
{

tyepedef float DataSet[5];

DataSet input[5] = {1.0, 2.0, 3.0, 4.0, 5.0};
int x;

for (x=0; x<=5; x++)
{
DataSet output[x] = x*2.0;

cout << output[x];

}


return 0;
}

Comments

  • The current declaration of output isn't what you want I don't believe.[code]DataSet input[5] = { 1.0, 2.0, 3.0, 4.0, 5.0 }, output[5] = { 0 };

    for(int i=0; i < 5; i++)
    output[i] = input[i] * 2.0;[/code]The second part is basically what is written above along with a cout statement. You may need to add iomanip and cout.setf(ios::showpoint) along with a call to setprecision .


    : This project is too simple but I am having a hard time understanding the question. What does he mean by "named Array type"?
    :
    :
    : // Write a declaration of a named array type, and then
    : // declare two arrays of that type. The array type should
    : // be called "DataSet", and the two two arrays should be called
    : // "input" and "output". Each array should hold five float values.
    : // Array "input" should be initialized in the declaration to
    : // contain 1.0, 2.0, 3.0, 4.0, and 5.0. Array "output" should be
    : // initialized by a For-loop to contain 2.0, 4.0, 6.0, 8.0, and 10.0
    : // (think about how to do that!).
    : //
    : // Write another For-loop that adds corresponding elements of two
    : // arrays and prints out the result.
    :
    :
    : THIS IS WHAT I HAVE TRIED FOR THE FIRST PART OF THE QUESTION. THE PROGRAM WILL NOT GIVE ME THE PROPER VALUES FOR THE OUTPUT, IN FACT IT DOES NOT COMPILE.
    :
    : #include
    :
    : using name space std;
    :
    : int main()
    : {
    :
    : tyepedef float DataSet[5];
    :
    : DataSet input[5] = {1.0, 2.0, 3.0, 4.0, 5.0};
    : int x;
    :
    : for (x=0; x<=5; x++)
    : {
    : DataSet output[x] = x*2.0;
    :
    : cout << output[x];
    :
    : }
    :
    :
    : return 0;
    : }
    :
  • That did not do the trick, it still does not compile.
    The following errors appear,
    "invalid operands of type 'float[5] amd double to binary operator"














    : The current declaration of output isn't what you want I don't believe.[code]DataSet input[5] = { 1.0, 2.0, 3.0, 4.0, 5.0 }, output[5] = { 0 };
    :
    : for(int i=0; i < 5; i++)
    : output[i] = input[i] * 2.0;[/code]The second part is basically what is written above along with a cout statement. You may need to add iomanip and cout.setf(ios::showpoint) along with a call to setprecision .
    :
    :
    : : This project is too simple but I am having a hard time understanding the question. What does he mean by "named Array type"?
    : :
    : :
    : : // Write a declaration of a named array type, and then
    : : // declare two arrays of that type. The array type should
    : : // be called "DataSet", and the two two arrays should be called
    : : // "input" and "output". Each array should hold five float values.
    : : // Array "input" should be initialized in the declaration to
    : : // contain 1.0, 2.0, 3.0, 4.0, and 5.0. Array "output" should be
    : : // initialized by a For-loop to contain 2.0, 4.0, 6.0, 8.0, and 10.0
    : : // (think about how to do that!).
    : : //
    : : // Write another For-loop that adds corresponding elements of two
    : : // arrays and prints out the result.
    : :
    : :
    : : THIS IS WHAT I HAVE TRIED FOR THE FIRST PART OF THE QUESTION. THE PROGRAM WILL NOT GIVE ME THE PROPER VALUES FOR THE OUTPUT, IN FACT IT DOES NOT COMPILE.
    : :
    : : #include
    : :
    : : using name space std;
    : :
    : : int main()
    : : {
    : :
    : : tyepedef float DataSet[5];
    : :
    : : DataSet input[5] = {1.0, 2.0, 3.0, 4.0, 5.0};
    : : int x;
    : :
    : : for (x=0; x<=5; x++)
    : : {
    : : DataSet output[x] = x*2.0;
    : :
    : : cout << output[x];
    : :
    : : }
    : :
    : :
    : : return 0;
    : : }
    : :
    :

  • The typedef is incorrect

    tyepedef float DataSet;
  • : The typedef is incorrect
    :
    : tyepedef float DataSet;
    :



    no luch after making the change
    -Mark
  • : The typedef is incorrect
    :
    : tyepedef float DataSet;
    :



    no luck after making the change
    -Mark
  • : : The typedef is incorrect
    : :
    : : tyepedef [blue]<-- stober? :P[/blue] float DataSet;
    : :
    :
    :
    :
    : no luck after making the change
    : -Mark
    :

    [blue]Hmm... I never cared for cout, but see if below will be of any use.[/blue][code]#include <iostream>
    #include

    using namespace std;

    typedef float DataSet;

    int main()
    {
    int x;
    DataSet input[] = { 1.0, 2.0, 3.0, 4.0, 5.0 },
    output[5] = { 0 };

    cout.setf(ios::showpoint);
    cout << setprecision(2);

    for(x = 0; x < 5; x++)
    output[x] = input[x] * 2.0f;

    for(x = 0; x < 5; x++)
    cout << output[x] << ' ';

    return (0);
    }[/code]
  • : : : The typedef is incorrect
    : : :
    : : : tyepedef [blue]<-- stober? :P[/blue] float DataSet;
    : : :
    : :
    : :
    : :
    : : no luck after making the change
    : : -Mark
    : :
    :
    : [blue]Hmm... I never cared for cout, but see if below will be of any use.[/blue][code]#include <iostream>
    : #include
    :
    : using namespace std;
    :
    : typedef float DataSet;
    :
    : int main()
    : {
    : int x;
    : DataSet input[] = { 1.0, 2.0, 3.0, 4.0, 5.0 },
    : output[5] = { 0 };
    :
    : cout.setf(ios::showpoint);
    : cout << setprecision(2);
    :
    : for(x = 0; x < 5; x++)
    : output[x] = input[x] * 2.0f;
    :
    : for(x = 0; x < 5; x++)
    : cout << output[x] << ' ';
    :
    : return (0);
    : }[/code]
    :






    That is perfect, and I see my mistake. I was declaring DataSet as an array itself instead of an array type that would define input and output.
    By the way what does the 'f' do in this statement?
    output[x] = input[x] * 2.0f;

    Thanks,
    Mark

  • : : : : The typedef is incorrect
    : : : :
    : : : : tyepedef [blue]<-- stober? :P[/blue] float DataSet;
    : : : :
    : : :
    : : :
    : : :
    : : : no luck after making the change
    : : : -Mark
    : : :
    : :
    : : [blue]Hmm... I never cared for cout, but see if below will be of any use.[/blue][code]#include <iostream>
    : : #include
    : :
    : : using namespace std;
    : :
    : : typedef float DataSet;
    : :
    : : int main()
    : : {
    : : int x;
    : : DataSet input[] = { 1.0, 2.0, 3.0, 4.0, 5.0 },
    : : output[5] = { 0 };
    : :
    : : cout.setf(ios::showpoint);
    : : cout << setprecision(2);
    : :
    : : for(x = 0; x < 5; x++)
    : : output[x] = input[x] * 2.0f;
    : :
    : : for(x = 0; x < 5; x++)
    : : cout << output[x] << ' ';
    : :
    : : return (0);
    : : }[/code]
    : :
    :
    :
    :
    :
    :
    :
    : That is perfect, and I see my mistake. I was declaring DataSet as an array itself instead of an array type that would define input and output.
    : By the way what does the 'f' do in this statement?
    : output[x] = input[x] * 2.0f;
    :
    : Thanks,
    : Mark
    :
    :


    The second part of the project asks us to write another For-loop that adds corresponding elements of two arrays and print out the result.
    I did the following and it works fine. But why doesn't the for loop work though?


    DataSet total[5] = { 0 };
    for(x = 0; x < 5; x++)
    total[x] = output[x] + input[x];
    cout<< endl;
    cout <<total[0]<<' '<<total[1]<<' '<<total[2]<<' '<<total[3]<<' '<<total[4]<<endl;


    /*for(x = 0; x < 5; x++)
    cout << endl; // this for loop does not work
    cout << total[x] << ' ';*/
  • : : By the way what does the 'f' do in this statement?
    : : output[x] = input[x] * 2.0f;

    [blue]Just telling the compiler that I wanted a float and not a double, which 2.0 is.[/blue]

    : The second part of the project asks us to write another For-loop that adds corresponding elements of two arrays and print out the result.
    : I did the following and it works fine. But why doesn't the for loop work though?

    [blue]*Probably* a consistency issue in the data type. For the first loop, it may be losing precision. What's the output all zeros? For the second one, it looks like you need braces - what does it do? print new lines, and then a single value? Try the bit below to see if it works.[/blue][code]#include

    using namespace std;

    typedef float DataSet;

    int main()
    {
    DataSet f1[] = { 1.1f, 2.2f, 3.3f, 4.4f, 5.5f },
    f2[] = { 6.6f, 7.7f, 8.8f, 9.9f, 0.0f },
    sum[5] = { 0 };

    for(int i=0; i < 5; i++)
    {
    sum[i] = f1[i] + f2[i];

    cout << "sum[" << i << "] = " << sum[i] << endl;
    }

    return (0);
    }[/code]
    :
    :
    : DataSet total[5] = { 0 };
    : for(x = 0; x < 5; x++)
    : total[x] = output[x] + input[x];
    : cout<< endl;
    : cout <<total[0]<<' '<<total[1]<<' '<<total[2]<<' '<<total[3]<<' '<<total[4]<<endl;
    :
    :
    : /*for(x = 0; x < 5; x++)
    : cout << endl; // this for loop does not work
    : cout << total[x] << ' ';*/
    :
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories