473,387 Members | 1,760 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,387 software developers and data experts.

something is wrong with my main function.

I am having this problem in multiple programs using pointers. The logic in the secondary function is correct but the answers come out wrong. If you can spot something not right, please help. I am using Visual Studio 2013 C++

The program is supposed to have a user input the length of an array and input the numbers used. Then the second function should find the largest and second to largest numbers in the array.

The code:

//program_homework_009_page_255_number_6_two_largest
//Purpose: To have a user input the numbers of an array and return the largest and second to largest number.

#include <stdio.h>

void find_two_largest(int a[], int n, int *largest, int *second_largest);

int main()
{
int x[100];
int y;
int big; //second to biggest
int bigger; //biggest
int i;

printf("\n Please input the desired length for array x:");
scanf_s("%d", &y);

for (i = 0; i <= y - 1; i++)
{
printf("\n Please enter the %d number for array x: ", i + 1);
scanf_s("%d", &x[i]);
}

find_two_largest(x, y, &bigger, &big);

printf("\n The largest number in your array is: %d, and the second to largest it:%d", bigger, big);

getchar();
getchar();

return 0;
}

void find_two_largest(int a[], int n, int *largest, int *second_largest)
{
int i;

*largest = a[0];
*second_largest = a[0];

for (i = 0; i <= n; i++)
{
if (a[i] < *largest)
{
*largest = a[i];
}
if (a[i] < *second_largest < *largest)
{
*second_largest = a[i];
}
}
}
May 7 '14 #1
2 1302
weaknessforcats
9,208 Expert Mod 8TB
Odd looking code here:

Expand|Select|Wrap|Line Numbers
  1. if (a[i] < *second_largest < *largest)
  2.  {etc...
What is it supposed to do?
May 7 '14 #2
donbock
2,426 Expert 2GB
By the way, you should validate y after the user enters it. The value of y must be between (1 or 2, your choice) and 100. However this has nothing to do with your current problem.
May 7 '14 #3

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

Similar topics

3
by: Kees Hoogendijk | last post by:
Hi everyone, I've tried for a few day to make a function to check out the zipcode. But it just doens't work. Can someone help me? In the zipcode, the figures must be larger than 1000 en the...
4
by: wongjoekmeu | last post by:
Hello All, >From a book where I have learned C++ it says that each application must have at least one function which is the main() function. Now I have the source code of a C++ program from...
6
by: davinci | last post by:
I had study it for a long time .but I didn't solve it .Ihad read <<Debuging with GDB>> But I didn't get something useful. include<iostream> #include<cstdlib> using std::isdigit; int main() { ...
89
by: Sweety | last post by:
hi, Is main function address is 657. its show in all compiler. try it & say why? bye,
7
by: Rex | last post by:
hai all, itz just a query,can i have two main function in a C program(1 file)
19
by: beliavsky | last post by:
I think I read a suggestion somewhere to wrap the code where a Python script starts in a main() function, so one has def main(): print "hi" main() instead of
5
by: PencoOdStip | last post by:
Do i have to have a main function? If i don't have a function called main,i get erors.Why do i have to have a function called main?
28
by: ravi | last post by:
Hello everybody, I am writing a small application which does some work before the user main function starts execution. I am trying to #define the main function. But the problem is that,
11
by: aarklon | last post by:
Hi all, I have heard many discussions among my colleagues that main is a user defined function or not. arguments in favour:- 1) if it is built in function it must be defined in some header...
4
by: pankaj do it | last post by:
hi what is the difference between the main() function of c and c++
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.