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 -
//assignment #5
-
#include<iostream>
-
#include<fstream>
-
using namespace std;
-
ifstream infile("data.dat");
-
const int VALUE=50;
-
void readdata(int, int[], int[]);
-
void printarray(int, int[]);
-
int smallest(int, int[]);
-
void construct(int , int [], int [], int []);
-
int whatshigher(int, int [], int [], int , int , int );
-
int main()
-
{
-
int n, small, small_1, small_diff, score1[VALUE], score2[VALUE];
-
int difference[VALUE], score1_count=0, score2_count=0, equal_count=0;
-
infile>>n;
-
-
readdata(n, score1, score2);
-
-
cout<<"Score 1 array is printing"<<endl;
-
printarray(n, score1);
-
cout<<"Score 2 array is printing"<<endl;
-
printarray(n, score2);
-
-
small=smallest(n, score1);
-
cout<<"smallest in array 1"<<endl;
-
cout<<small<<endl;
-
-
small_1=smallest(n, score2);
-
cout<<"smallest in array 2"<<endl;
-
cout<<small_1<<endl;
-
-
construct(n, score1, score2, difference);
-
cout<<"The smallest in difference: ";
-
small_diff=smallest(n, difference);
-
cout<<small_diff<<endl;
-
-
whatshigher(n, score1, score2, score1_count, score2_count, equal_count);
-
cout<<"Array score1 was larger "<<score1_count<<"times"<<endl;
-
cout<<"Array score2 was larger "<<score2_count<<"times"<<endl;
-
cout<<"The two arrays were equal"<<equal_count<<"times"<<endl;
-
-
-
-
-
-
-
-
-
infile.close();
-
-
system("pause");
-
return 0;
-
}
-
-
//readdata()
-
void readdata(int n, int nums1[], int nums2[]){
-
-
for(int i=0; i<n; i++){
-
infile>>nums1[i];
-
infile>>nums2[i];
-
}
-
-
-
}
-
-
-
-
-
-
//printarray()
-
void printarray(int lim, int arr[]){
-
for(int i=0; i<lim; i++)
-
cout<<arr[i]<<" ";
-
-
cout<<endl;
-
-
-
-
}
-
-
-
-
//smallest()
-
int smallest(int n, int nums[]){
-
//Process array
-
int min ;
-
for(int i=0; i<n; i++)
-
{ int num = nums[i];
-
if(num < min)
-
min = num;
-
}
-
-
return min;
-
}
-
-
-
//construct()
-
void construct(int k, int old1[], int old2[], int diff[]){
-
-
cout<<"The difference array: ";
-
for(int i=0; i<k; i++){
-
diff[i]=old1[i]-old2[i];
-
cout<<diff[i]<<" ";
-
}
-
cout<<endl;
-
}
-
-
-
//whatshigher()
-
int whatshigher(int n, int score1[], int score2[], int c1, int c2, int c3){
-
-
-
-
for(int i=0; i<n; i++){
-
if(score1[i]>score2[i]){
-
cout<<"In position "<<i<<", score1 is larger:"
-
<<score1[i]<<" is greater than "<<score2[i]<<endl;
-
c1++;
-
}
-
else if(score2[i]>score1[i]){
-
c2++;
-
cout<<"In position "<<i<<", score2 is larger:"
-
<<score2[i]<<" is greater than "<<score1[i]<<endl;
-
-
}
-
else if(score1[i]==score2[i]){
-
cout<<"In position "<<i<<", the two arrays have the same value: "
-
<<score1[i]<<endl;
-
c3++;
-
}
-
-
}
-
-
return c1, c2, c3;
-
-
}
-
-
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 . . .
1 1711
Thie code:
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
|
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...
|
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
{
...
|
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>...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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,...
|
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,...
|
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...
|
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...
|
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,...
| |