pointer arithmetic

Discussion in 'C++' started by jdcrief, Aug 31, 2006.

  1. jdcrief

    jdcrief Guest

    I have all of this program working except for the pointer arithmetic
    part, I think. This program should average the student's grades
    together using pointer arithmetic and then display the student's and
    their average grade. I do know that I will need to delete the array to
    avoid a memory leak, but I am stuck on this part of the program. I am
    open to any suggestions.

    #include "stdafx.h"
    #include <iostream>
    #include <stdio.h>

    using std::cin;
    using std::cout;
    using std::endl;

    int main ()
    {
    int max;
    int index;
    int result;
    int sum;
    int avg;

    cout<<"How many grades will you be entering? ";
    cin>>max;

    for (index = 0; index < max; index++)
    {
    cout << "\nPlease Enter Grades: ";

    // store value into numbers array

    cin >>numbers[index];

    cout<<index+1<<". "<<numbers[index]<<endl;

    sum =sum +numbers[index];
    }

    cout<<"\nStudent's Grade Total: "<<sum;

    avg = sum/max;

    cout<<"\nStudent's Grade Average: "<<avg<<endl;

    return 0;
    }
     
    jdcrief, Aug 31, 2006
    #1
    1. Advertisements

  2. jdcrief wrote:
    > I have all of this program working except for the pointer arithmetic
    > part, I think. This program should average the student's grades
    > together using pointer arithmetic and then display the student's and
    > their average grade. I do know that I will need to delete the array
    > to avoid a memory leak, but I am stuck on this part of the program.
    > I am open to any suggestions.
    >
    > #include "stdafx.h"
    > #include <iostream>
    > #include <stdio.h>
    >
    > using std::cin;
    > using std::cout;
    > using std::endl;
    >
    > int main ()
    > {
    > int max;
    > int index;
    > int result;
    > int sum;
    > int avg;
    >
    > cout<<"How many grades will you be entering? ";
    > cin>>max;
    >
    > for (index = 0; index < max; index++)
    > {
    > cout << "\nPlease Enter Grades: ";
    >
    > // store value into numbers array
    >
    > cin >>numbers[index];


    I am looking and I am looking... And I can't find "numbers" anywhere
    before this line. Did you forget to define it?

    >
    > cout<<index+1<<". "<<numbers[index]<<endl;
    >
    > sum =sum +numbers[index];
    > }
    >
    > cout<<"\nStudent's Grade Total: "<<sum;
    >
    > avg = sum/max;
    >
    > cout<<"\nStudent's Grade Average: "<<avg<<endl;
    >
    > return 0;
    > }


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 31, 2006
    #2
    1. Advertisements

  3. jdcrief

    David Harmon Guest

    On 30 Aug 2006 18:30:42 -0700 in comp.lang.c++, "jdcrief"
    <> wrote,
    >I have all of this program working except for the pointer arithmetic
    >part, I think. This program should average the student's grades
    >together using pointer arithmetic and then display the student's and
    >their average grade.


    Is the "pointer arithmetic" mandated by a school assignment?
    Because otherwise, as you have probably noticed, you have no need of
    such a thing just to compute the average. And indeed, you have none
    if it in the code sample you posted.

    >I do know that I will need to delete the array to
    >avoid a memory leak,


    You should delete everything you new, but you didn't do that either
    yet.

    A real C++ programmer would certainly choose std::vector over
    pointer arithmetic and new and delete if at all possible.

    > cout<<"How many grades will you be entering? ";
    > cin>>max;


    This makes things tedious for your user; preferably something like

    while(cin >> grade) {
    ++max; // count it
     
    David Harmon, Aug 31, 2006
    #3
  4. jdcrief

    jdcrief Guest

    Thanks David...you are right, i'm not a "REAL" C++ programmer. Again,
    thanks for you help.
    David Harmon wrote:
    > On 30 Aug 2006 18:30:42 -0700 in comp.lang.c++, "jdcrief"
    > <> wrote,
    > >I have all of this program working except for the pointer arithmetic
    > >part, I think. This program should average the student's grades
    > >together using pointer arithmetic and then display the student's and
    > >their average grade.

    >
    > Is the "pointer arithmetic" mandated by a school assignment?
    > Because otherwise, as you have probably noticed, you have no need of
    > such a thing just to compute the average. And indeed, you have none
    > if it in the code sample you posted.
    >
    > >I do know that I will need to delete the array to
    > >avoid a memory leak,

    >
    > You should delete everything you new, but you didn't do that either
    > yet.
    >
    > A real C++ programmer would certainly choose std::vector over
    > pointer arithmetic and new and delete if at all possible.
    >
    > > cout<<"How many grades will you be entering? ";
    > > cin>>max;

    >
    > This makes things tedious for your user; preferably something like
    >
    > while(cin >> grade) {
    > ++max; // count it
     
    jdcrief, Aug 31, 2006
    #4
  5. jdcrief

    jdcrief Guest

    Can anyone explain why this will run using the DevC++ compiler, but not
    the Visual 2005 express?

    // Name : John D. Moncrief
    // Date : 8/25/06
    // Description : Assignment 4 - Problem 2: Pointer Arithmetic
    // Assignment/Problem : A4P2
    // File Name : jmoncriefA4P2
    // Course : Intro to C++ CS2244
    // Instructor : Raymond Welch

    #include "stdafx.h"
    #include <iostream>
    #include <iomanip>

    using std::cout;
    using std::endl;
    using std::cin;

    int main ()
    {
    int max;
    int index;
    int result;
    int sum;
    int numbers[index];
    int avg;

    cout<<"How many grades will you be entering? ";
    cin>>max;

    for (index = 0; index < max; index++)
    {
    cout << "\nPlease Enter Grades: ";

    // store value into numbers array

    cin >>numbers[index];
    cout<<index+1<<". "<<numbers[index]<<endl;
    sum =sum +numbers[index];
    }

    cout<<"\nStudent's Grade Total: "<<sum;
    avg = sum/max;
    cout<<"\nStudent's Grade Average: "<<avg<<endl;
    return 0;
    }
    jdcrief wrote:
    > Thanks David...you are right, i'm not a "REAL" C++ programmer. Again,
    > thanks for you help.
    > David Harmon wrote:
    > > On 30 Aug 2006 18:30:42 -0700 in comp.lang.c++, "jdcrief"
    > > <> wrote,
    > > >I have all of this program working except for the pointer arithmetic
    > > >part, I think. This program should average the student's grades
    > > >together using pointer arithmetic and then display the student's and
    > > >their average grade.

    > >
    > > Is the "pointer arithmetic" mandated by a school assignment?
    > > Because otherwise, as you have probably noticed, you have no need of
    > > such a thing just to compute the average. And indeed, you have none
    > > if it in the code sample you posted.
    > >
    > > >I do know that I will need to delete the array to
    > > >avoid a memory leak,

    > >
    > > You should delete everything you new, but you didn't do that either
    > > yet.
    > >
    > > A real C++ programmer would certainly choose std::vector over
    > > pointer arithmetic and new and delete if at all possible.
    > >
    > > > cout<<"How many grades will you be entering? ";
    > > > cin>>max;

    > >
    > > This makes things tedious for your user; preferably something like
    > >
    > > while(cin >> grade) {
    > > ++max; // count it
     
    jdcrief, Sep 1, 2006
    #5
  6. jdcrief wrote:
    > Can anyone explain why this will run using the DevC++ compiler, but not
    > the Visual 2005 express?
    >
    > // Name : John D. Moncrief
    > // Date : 8/25/06
    > // Description : Assignment 4 - Problem 2: Pointer Arithmetic
    > // Assignment/Problem : A4P2
    > // File Name : jmoncriefA4P2
    > // Course : Intro to C++ CS2244
    > // Instructor : Raymond Welch
    >
    > #include "stdafx.h"


    Delete the above line. You don't need it.

    > #include <iostream>
    > #include <iomanip>
    >
    > using std::cout;
    > using std::endl;
    > using std::cin;
    >
    > int main ()
    > {
    > int max;
    > int index;
    > int result;
    > int sum;
    > int numbers[index];


    The above line is not legal C++, but it is part of a gcc extension.
    DevC++ is an IDE that uses gcc. What exactly is the above line
    supposed to do? And exactly what value is index right here?

    To avoid this problem in the future, always compile in gcc using -Wall
    -Wextra -pedantic, which turns off the extensions and gives you most of
    the useful warnings.

    Best regards,

    Tom
     
    Thomas Tutone, Sep 1, 2006
    #6
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. a

    pointer arithmetic

    a, Sep 11, 2003, in forum: C++
    Replies:
    6
    Views:
    699
  2. Replies:
    10
    Views:
    1,003
    Chris Torek
    Feb 4, 2005
  3. joshc
    Replies:
    5
    Views:
    775
    Keith Thompson
    Mar 31, 2005
  4. jimjim
    Replies:
    16
    Views:
    1,123
    Jordan Abel
    Mar 28, 2006
  5. Replies:
    4
    Views:
    1,664
    Fred Zwarts
    Jul 2, 2009
  6. A
    Replies:
    7
    Views:
    816
  7. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    793
    James Kuyper
    Sep 23, 2011
  8. arnuld

    pointer vs pointer to pointer

    arnuld, Jun 11, 2012, in forum: C Programming
    Replies:
    4
    Views:
    658
    Ben Bacarisse
    Jun 11, 2012
Loading...