Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

summing up an array

Posted on 2000-04-27
11
Medium Priority
?
222 Views
Last Modified: 2010-04-02
I need to create a function that sums up a 2x7 array.  Can someone help?
0
Comment
Question by:nationnon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 1

Accepted Solution

by:
xelpmoc2 earned 200 total points
ID: 2756239
int sum = 0;
int array [2][7];

for (int i = 0; i<2;i++)
{
   for (int j = 0; j<7; j++)
   {
      sum = array[i][j];
   }
}
0
 
LVL 1

Author Comment

by:nationnon
ID: 2756338
Adjusted points from 25 to 50
0
 
LVL 1

Author Comment

by:nationnon
ID: 2756339
can you look at this and see what i'm doing wrong?



#include <iostream.h>
#include <fstream.h>

int sum = 0;
int array [2][7];

int sum()
{
for (int i = 0; x<2;x++)
{
   for (int j = 0; y<7; j++)
   {
      sum = array[i][j];
   }
}

}


      int smallArray()
{
int smallest=array[0];
      for(int x=1;x<array_size;x++)
      {if (smallest>array[x])smallest=array[x];}
      cout<<smallest;
}

void main()

//Declare and initialize array
{
int num[2][7] = {0,0};

int hold[2][7];

//Load 14 integers into aray
for (int x =0; x < 2; ++x)
{
     for (int y = 0; y < 7; ++y)
     {
         cout << "Press a whole number then hit enter ";
         cin  >> hold [x][y];
     }
}

}

// Biggest array function
{
int bigArray()

int biggest=array[0];
      for(int x=1;x<array_size;x++)
      {if (biggest<array[x])biggest=array[x];}
      cout<<biggest;

cout << "Choose 1 for biggest number ";
cout << "Choose 2 for smallest number ";
cout << "Choose 3 for the sum of all numbers in array ";
cin >> menu;

if (menu==1)
cout << bigArray() << endl;
    cout <<  endl;

if (menu==2)
    cout << smallArray() << endl;
      cout << endl;

if (menu==3)
      cout << sumArray() <<endl;
      cout << endl;
}
      int smallArray()
{
int smallest=array[0];
      for(int x=1;x<array_size;x++)
      {if (smallest>array[x])smallest=array[x];}
      cout<<smallest;
}
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Expert Comment

by:xelpmoc2
ID: 2756550
I made a slight mistake in the code I sent you and I just caught it in mine and in yours.  
int sum = 0;
int array [2][7];

for (int i = 0; i<2;i++)
{
   for (int j = 0; j<7; j++)
   {
      sum += array[i][j];
   }
}


You, (we) cannot just say:
sum = array[i][j]
because then we are just reassigning sum the value in the array at the values of i and j and will ultimately just end up with sum = the value at array[2][7] since that will be the last  number that goes through.  We have to increment sum each time through the for loop to add the values you want,
sum = array[0][0]+array[0][1]+array[0][2].....+ array[2][6] + array[2][7]

Try fixing your sum function to the above and let me know how it works.

0
 
LVL 1

Author Comment

by:nationnon
ID: 2756683
I'm still getting errors
0
 
LVL 1

Author Comment

by:nationnon
ID: 2757003
I got one error

-------------------------

missing function header (old-style formal list?)

and it points to the first bracket.
0
 
LVL 1

Expert Comment

by:xelpmoc2
ID: 2757213
please send me the code you have as of now.
0
 
LVL 1

Expert Comment

by:xelpmoc2
ID: 2757249
You are making this program much more difficult then it needs to be.  I would suggest starting over as painful as that sounds.  You need to unit test as you go.  For example, first write a program that reads in a 2X7 array and output it to make sure that part works, then try summing the array, once that is tested then try to find the largest integer and so on.  It looks like you wrote all of the code in one lump...that is NEVER a good idea when you program, it makes tasks like debugging very difficult because you cannot eliminate any one area.  Does this make sense?
0
 
LVL 1

Expert Comment

by:xelpmoc2
ID: 2757269
Here is the function definition for your sum array:

// declaration
int sumarray(int array[][]);

// definition
int sumarray (array[][])
{
   for (int i = 0; i<2;i++)
   {
      for (int j = 0; j<7; j++)
      {
        sum = sum + array[i][j];
      }
   }
}

You have to pass in an array, you thus it has to be in the declaration and the definition.  This is the full explanation of how to sum a 2X7 array.



 
0
 
LVL 1

Expert Comment

by:xelpmoc2
ID: 2757306
I'm feeling generous, to fix this function

/* declaration; void since you are coutint the value you are not actually passing it */

void smallArray(array[2][7]);

// define the function:

void smallArray(array[][])
{
// give smallest some huge number
int smallest =100000000;
// loop through the array to search
for(int x=0; x<2; x++)
{
   for (int y = 0; y< 7; y++)
   {
     if (smallest < array[x][y])  
         smallest=array[x][y];
   }
}
// to output value to screen
cout <<  smallest;

/* to return the value out of the function- need to make the void an integer if you do this*/
return smallest;
}

0
 
LVL 1

Author Comment

by:nationnon
ID: 2759665
yes, and you're right
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question