Computers & Internet Logo

Related Topics:

Posted on May 07, 2009
Answered by a Fixya Expert

Trustworthy Expert Solutions

At Fixya.com, our trusted experts are meticulously vetted and possess extensive experience in their respective fields. Backed by a community of knowledgeable professionals, our platform ensures that the solutions provided are thoroughly researched and validated.

View Our Top Experts

Program of c to find largest and second larhest number of given 50 numbers using array

1 Answer

Anonymous

Level 2:

An expert who has achieved level 2 by getting 100 points

MVP:

An expert that got 5 achievements.

Governor:

An expert whose answer got voted for 20 times.

Hot-Shot:

An expert who has answered 20 questions.

  • Expert 54 Answers
  • Posted on May 07, 2009
Anonymous
Expert
Level 2:

An expert who has achieved level 2 by getting 100 points

MVP:

An expert that got 5 achievements.

Governor:

An expert whose answer got voted for 20 times.

Hot-Shot:

An expert who has answered 20 questions.

Joined: May 06, 2009
Answers
54
Questions
0
Helped
15602
Points
106

This code generates some random number to test.


#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{ findLargest();
return 0;
}

int findLargest()
{ int someNumbers[50];
int i;

// to generate some numbers for us to test
for(i=0; i<50; i++)
// generate a random number between 0-100
someNumbers[i] = rand() % 100;

// for keeping track of numbers, set as smallest possible
int largest = INT_MIN;
int largest2 = INT_MIN;

// go through each item in the array
for(i=0; i<50; i++)
{ // if bigger than our previous max, set as new max
if (someNumbers[i] > someNumbers[largest])
largest = i;
// if it's not been set as new max, and is bigger than current 2nd largest
else if(someNumbers[i] > someNumbers[largest2])
largest2 = i;

// for printing all numbers in the array
printf("%d | %d\n",i, someNumbers[i]);
}

// print largest numbers and their position in the array
printf("largest %d (pos %d).\n2nd largest %d (pos %d)",
someNumbers[largest],
largest,
someNumbers[largest2],
largest2
);

return 0;

}

Add Your Answer

×

Uploading: 0%

my-video-file.mp4

Complete. Click "Add" to insert your video. Add

×

Loading...
Loading...

Related Questions:

0helpful
1answer

Write a program c that declares an array of 100 elements to hold these values. Read the values in from the file and store them in successive elements of the array. Keep track of how many numbers are read...

That appears to be an intermediate question in a programming class. Best read the book, the answer is not what is important the process of getting the answer is what you are trying to learn.
0helpful
1answer

I have to tutor tasm.exe and i need sample programs with explanation

Write a program to find out the largest number from a
given unordered array of 8-bit numbers. Store the largest
number in largest variable.


52h, 23h, 56h, 45h, 9Ah, ABh

0helpful
1answer

Make a program that will accept tthe size of an array (number must be greater than). Enter and fill the array with numbers then sort in descending order (largest to smallest). Display the unsorted and...


coding in C++

#include<iostream.h>
#include<conio.h>
#define max 50

void main()
{
clrscr();
int arr[max],size=0;
int i=0,j=0,temp;
do
{
cout<<"Enter the size of array(less than or equal to 50)";
cin>>size;
} while(size>50);

for(i=0;i<size;i++){
cout<<"Enter a number";
cin>>arr[i];
}


for( i=1;i<size;i++){
for(j=0;j<size;j++){
if(arr[j+1]>arr[j]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;


}

}
}
cout<<”Array in descending order”;
for(i=0;i<size;i++){
cout<<arr[i]<<"\n";
}
getch();
return 0;
}
0helpful
1answer

C++

http://www.cplusplus.com/doc/tutorial/
0helpful
1answer

Please Solve this C++ Program

Write a program to prompt the user to input several lines of input. A line of inputs is terminated by '*'. For each readline of input the program should output:(use pointer notation for the array) . The total # of words . The lenght of the longest word in the line At the end the program should output: . The total number of words read in all lines of input . The lenght of the longest word read in all lines of input
0helpful
1answer

Programs with variable number of arguments

To start points and lines are not shapes, and a triangle is not representable with a single function, therefore you need a function to draw shapes , given a number of points as parameters, and one to draw math functions (lines etc.).

Here is an example: Draw Line, Ellipse, Polygon ...

From website above, here is script in JS:

"<script type="text/javascript">
<!--
function myDrawFunction()
{
jg_doc.setColor("#00ff00"); // green
jg_doc.fillEllipse(100, 200, 100, 180); // co-ordinates related to the document
jg_doc.setColor("maroon");
jg_doc.drawPolyline(new Array(50, 10, 120), new Array(10, 50, 70));
jg_doc.paint(); // draws, in this case, directly into the document

jg.setColor("#ff0000"); // red
jg.drawLine(10, 113, 220, 55); // co-ordinates related to "myCanvas"
jg.setColor("#0000ff"); // blue
jg.fillRect(110, 120, 30, 60);
jg.paint();

jg2.setColor("#0000ff"); // blue
jg2.drawEllipse(10, 50, 30, 100);
jg2.drawRect(400, 10, 100, 50);
jg2.paint();
}

var jg_doc = new jsGraphics(); // draw directly into document
var jg = new jsGraphics("myCanvas");
var jg2 = new jsGraphics("anotherCanvas");

myDrawFunction();

//-->
</script>"
0helpful
1answer

I have a problem

This is a common starter challenge in college and high school programming classes. While i'm not willing to code it for you, I can show you the methods you'll need to understand in order to approach the problem. The easiest way of doing this is by using a brute force method. Just keep testing the number to see if the remainder after dividing it with a number lower than it is zero. If so, it's a factor.
For example, If the number is 35 then pick a number just one lower than it that's not the number 1. Now divide the two numbers. 35/34 = a number with many decimal places after it, which means its not one of the factors of the number. However, eventually, you'll approach the number 7, and 35/7 = 5. This number has no decimal, meaning it IS a factor of 35, and also the largest non-zero factor of 35. Your algorithm can now stop, since it found the largest non-zero number.
Here's some pseudo-code:
int largestfactor(int number) {
int temp = number;
for(temp - 1; temp>=1; temp--) {
if (number % temp == 0) return temp
}
return temp
}
Something like that, with some bug tweaks will do it. The "%" sign is called a modulus. It's a standard c++ operator. Good luck, and remember to vote Fixya if you liked it.
The wikipedia article that helps you with better and more effecient algorithms can be found here.
Steven
0helpful
2answers

Source code for word count

Haha, i wrote one of thee a while ago for a competition. You need to us e the char at function mate :D
Inside a for loop
Ill havea look for you though :D
Not finding what you are looking for?

374 views

Ask a Question

Usually answered in minutes!

Top Computers & Internet Experts

Grand Canyon Tech
Grand Canyon Tech

Level 3 Expert

3867 Answers

Brad Brown

Level 3 Expert

19187 Answers

Cindy Wells

Level 3 Expert

6688 Answers

Are you a Computer and Internet Expert? Answer questions, earn points and help others

Answer questions

Manuals & User Guides

Loading...