473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

How come my counter isnt working?

I need to print my counters in the main program and not in the function but I cant figure out why I the main program prints 0's and when I test it in the function the counters are correct.

I have posted my code and output below. The parts i'm having trouble with are bold and underlined. Thanks



Expand|Select|Wrap|Line Numbers
  1. //assignment #5
  2. #include<iostream>
  3. #include<fstream>
  4. using namespace std;
  5. ifstream infile("data.dat");
  6. const int VALUE=50;
  7. void readdata(int, int[], int[]);
  8. void printarray(int, int[]);
  9. int smallest(int, int[]);
  10. void construct(int , int [], int [], int []);
  11. int whatshigher(int, int [], int [], int , int , int );
  12. int main()
  13. {
  14.     int n, small, small_1, small_diff, score1[VALUE], score2[VALUE];
  15.     int difference[VALUE], score1_count=0, score2_count=0, equal_count=0;
  16.     infile>>n;
  17.  
  18.     readdata(n, score1, score2);
  19.  
  20.     cout<<"Score 1 array is printing"<<endl;
  21.     printarray(n, score1);
  22.     cout<<"Score 2 array is printing"<<endl;
  23.     printarray(n, score2);
  24.  
  25.     small=smallest(n, score1);
  26.     cout<<"smallest in array 1"<<endl;
  27.     cout<<small<<endl;
  28.  
  29.     small_1=smallest(n, score2);
  30.     cout<<"smallest in array 2"<<endl;
  31.     cout<<small_1<<endl;
  32.  
  33.     construct(n, score1, score2, difference);
  34.     cout<<"The smallest in difference: ";
  35.     small_diff=smallest(n, difference);
  36.     cout<<small_diff<<endl;
  37.  
  38.     whatshigher(n, score1, score2, score1_count, score2_count, equal_count); 
  39.     cout<<"Array score1 was larger "<<score1_count<<"times"<<endl;
  40.     cout<<"Array score2 was larger "<<score2_count<<"times"<<endl;
  41.     cout<<"The two arrays were equal"<<equal_count<<"times"<<endl;
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.     infile.close();
  51.  
  52. system("pause");
  53. return 0;
  54. }
  55.  
  56. //readdata()
  57. void readdata(int n, int nums1[], int nums2[]){
  58.  
  59. for(int i=0; i<n; i++){
  60.      infile>>nums1[i];
  61.      infile>>nums2[i];
  62. }
  63.  
  64.  
  65. }
  66.  
  67.  
  68.  
  69.  
  70.  
  71. //printarray()
  72. void printarray(int lim, int arr[]){
  73. for(int i=0; i<lim; i++)
  74. cout<<arr[i]<<" ";
  75.  
  76. cout<<endl;
  77.  
  78.  
  79.  
  80. }
  81.  
  82.  
  83.  
  84. //smallest()
  85. int smallest(int n, int nums[]){
  86. //Process array
  87. int min ;
  88. for(int i=0; i<n; i++)
  89. { int num = nums[i];
  90. if(num < min)
  91. min = num;
  92. }
  93.  
  94. return min;
  95. }
  96.  
  97.  
  98. //construct()
  99. void construct(int k, int old1[], int old2[], int diff[]){
  100.  
  101.     cout<<"The difference array: ";
  102.     for(int i=0; i<k; i++){
  103.          diff[i]=old1[i]-old2[i];
  104.          cout<<diff[i]<<" ";
  105.          }
  106.     cout<<endl;
  107. }
  108.  
  109.  
  110. //whatshigher()
  111. int whatshigher(int n, int score1[], int score2[], int c1, int c2, int c3){
  112.  
  113.  
  114.  
  115.      for(int i=0; i<n; i++){
  116.           if(score1[i]>score2[i]){
  117.                cout<<"In position "<<i<<", score1 is larger:"
  118.                    <<score1[i]<<" is greater than "<<score2[i]<<endl;
  119.                c1++;
  120.           }
  121.           else if(score2[i]>score1[i]){
  122.                c2++;
  123.                cout<<"In position "<<i<<", score2 is larger:"
  124.                    <<score2[i]<<" is greater than "<<score1[i]<<endl;
  125.  
  126.                }
  127.           else if(score1[i]==score2[i]){
  128.                cout<<"In position "<<i<<", the two arrays have the same value: "
  129.                    <<score1[i]<<endl;
  130.                c3++;
  131.                }
  132.  
  133.      }
  134.  
  135.      return c1, c2, c3;
  136.  
  137. }
  138.  
  139.  

Here is my output:

Score 1 array is printing
12 1 51
Score 2 array is printing
45 15 60
smallest in array 1
1
smallest in array 2
15
The difference array: -33 -14 -9
The smallest in difference: -33
In position 0, score2 is larger:45 is greater than 12
In position 1, score2 is larger:15 is greater than 1
In position 2, score2 is larger:60 is greater than 51
Array score1 was larger 0times
Array score2 was larger 0times
The two arrays were equal 0times

Press any key to continue . . .
Nov 12 '11 #1
1 1711
weaknessforcats
9,208 Expert Mod 8TB
Thie code:
Expand|Select|Wrap|Line Numbers
  1. return c1, c2, c3;
isn't doing what you think.

You can return only one value.

The comma operator executes the code between the commas. The result of each comma step is discarded. So, c1, ->get c1 do nothing, discard
Then c2, -> get c2 do nothing then discard. Then c3-> get c3 and return it.

If you need to retreive multiple values from a function you need output arguments. That is, pass in the address of the variables and have the function change the variables using the address. That is, use pointer arguments.
Nov 13 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: John | last post by:
I am running the WebService / CurrentConnections performance counter. It works fine for one site I'm looking at, but not for another. The one it doesn't work for, the polling vertical red line...
4
by: Rick | last post by:
Hi, I just installed IIS and when I tried browsing the link: http://localhost it says: "you are not authorized to view this page" HTTP Error 403 - Forbidden
2
by: DHarry | last post by:
Hello, I wrote an application in c# .net 2.0. The application monitors the network speed of a networkinterface. If I install the application at another pc with the .net framework installed, the...
2
by: wudoug119 | last post by:
This is my code and it will take any number that I input and say it is a prime number. Please help me... int Prime(int prime) //declares isPrime as a function using integers { ...
1
by: hackerboy9523 | last post by:
this is what i am typing and it doesnt come up as code (i am using it on myspace)it just displays the code without the effect! <table> <tr><td>name</td><td>Game 1</td><td>game 2</td</tr>...
5
by: soheir | last post by:
am doing my first project so plz help i've created a submit button <input type="submit" onclick="valid()"> my function valid checks if the fields are not empty if empty a window prmpt appears...
0
by: John Sheppard | last post by:
Hello there, I have a checked list box which I am trying to save to my dataset. The last tickbox I edit before saving, doesnt actually get saved to the underlying binding source. I have...
2
by: nse111 | last post by:
I am trying to get some data and show it usig paging. my coding displays and also the paging is also visible on my web page. but it doesnt work. as in when i click next the next set of results aren't...
1
by: dougancil | last post by:
I have the following code: Imports System.IO Imports System.Data Imports System.Data.SqlClient Partial Class _Default Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.