473,668 Members | 2,690 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to find index of a maximum number in an array?

7 New Member
i need to find the max position of my program. how can i do that?
Expand|Select|Wrap|Line Numbers
  1. int main() 
  2.         int j[10],max=0,k; 
  3. for(k=0;k<=9;k++) 
  4.         { 
  5.         printf("Enter the number:"); 
  6.         scanf("%d", &j[k]); 
  7.         }     
  8.         for(k=0;k<=9;k++) 
  9.     { 
  10.         printf("x[%d]=%d\n", k, j[k]); 
  11.         if(max<j[k]) 
  12.         { 
  13.             max=j[k]; 
  14.         } 
  15.     } 
  16.         printf("The maximum number is %d\n",max); 
  17. }
  18.  
May 8 '10 #1
6 17813
Dheeraj Joshi
1,123 Recognized Expert Top Contributor
You mean to say, you need to figure out the index of a maximum number in an array?

Whats the big deal in it? When you calculate the maximum you assign a position variable to index of the loop. When you are done executing the loop you will also have the position in the array where maximum number is present.

Expand|Select|Wrap|Line Numbers
  1. for(k=0;k<=9;k++) 
  2.     if(max<j[k]) 
  3.     {  
  4.        max=j[k]; 
  5.        iMaxPosition = k;
  6.     } 
  7.  
Regards
Dheeraj Joshi
May 8 '10 #2
abhinuke
23 New Member
Slightly different query but I am using index of variable from array,thats why I am posting it here.

I have this code by which I have to trim the successive two or more white spaces in a string.
For ex.If I enter
"India(2WS)wins (2WS)Soccer(2WS )World(2WS)Cup! !"
Here 2WS refers to 2 white spaces.
then output should be
"India wins Soccer World Cup!!".

Here is the code I have been working on.I couldn't do it in C.Thats why using library functions.

Every time I run this thing its giving me segmentation fault.

Expand|Select|Wrap|Line Numbers
  1. #include <stdlib.h>
  2. #include<iostream>
  3. #include<string>
  4. using namespace std;
  5.  
  6. int main()
  7. char a[30];
  8.     string s1;
  9.     int b[5];
  10.     cout<<"Enter desired string\n";
  11.     cin.getline(a,30);
  12.     s1=a;
  13.     int n=0,i;
  14.     for(i=0;i<30;i++)
  15.     {
  16.         if(a[i]==' '&& a[i+1]==' ')
  17.         {
  18.             n++;
  19.             b[i]=i;
  20.         }
  21.  
  22.    }
  23.     cout<<"The white spaces in given string are "<<n;
  24.     cout<<"\n";
  25.     i=0;
  26. while(i<n)
  27.     {
  28.         s1.erase(b[i],1);
  29.         i++;
  30.     }
  31.  
  32.  
  33.    cout<<"\nThe new formatted string is \n";
  34.    cout<<s1;
  35.    return 0;
  36. }
  37.  
Any suggestions,hin ts?
Thanks in advance.
Cheers.
Jun 19 '10 #3
whodgson
542 Contributor
Since you do not know the size of the string, you will need to establish that, with the standard library function sizeof() and use this as the for loop ending condition. There may be more problems in your code which I have not looked for.
Jun 20 '10 #4
Joseph Martell
198 Recognized Expert New Member
This loop is guaranteed to run over the bounds of a:


Expand|Select|Wrap|Line Numbers
  1. for(i=0;i<30;i++) 
  2.     { 
  3.         //when i = 29, (i+1) = 30!
  4.         if(a[i]==' '&& a[i+1]==' ') 
  5.         { 
  6.             n++; 
  7.             b[i]=i; 
  8.         } 
  9.  
  10.    } 
Also, I belive you should to use

Expand|Select|Wrap|Line Numbers
  1. b[n]=i;
  2. n++;
instead of

Expand|Select|Wrap|Line Numbers
  1. n++;
  2. b[i]=i;
using 'i' as your index means you are pretty much guaranteed to overrun the bounds of b as well.

I dont' know if these would cause your segmentation fault, but they certainly won't help.
Jun 21 '10 #5
donbock
2,426 Recognized Expert Top Contributor
What if the array contains only negative numbers? The initial value of max should be j[0], not 0. Likewise, the initial value of iMaxPosition should be 0.
Jun 21 '10 #6
santechselva
8 New Member
@ashiela
#include<stdio. h>
#include<conio. h>
void main()
{
int i=0,j,k,l,m;
char a[20];
clrscr();
for(k=0;k<4;k++ )
{
printf("enter value for sorting");
scanf("%s",&a[k]);
}
for(i=0;i<4;i++ )
{
for(j=i+1;j<4;j ++)
{
if(a[i]>a[j])
{
l=a[i];
a[i]=a[j];
a[j]=l;
}
}
}
for(m=0;m<4;m++ )
{
printf("\n%c",a[m]);
}
getch();
}
Jun 23 '10 #7

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

Similar topics

2
14159
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip Smarty-2.6.7.tar.gz on a system running WindowsXP SP2. Apache and PHP tested out fine. After adding Smarty, I ran the following http://localhost/testphp.php
22
3593
by: edgrsprj | last post by:
PROPOSED EARTHQUAKE FORECASTING COMPUTER PROGRAM DEVELOPMENT EFFORT Posted July 11, 2005 My main earthquake forecasting Web page is: http://www.freewebz.com/eq-forecasting/Data.html Newsgroup Readers: If you circulate copies of this report to groups of computer programmers at different universities etc. around the world then they might find the subject matter to be interesting.
0
6117
by: Tom Lee | last post by:
Hi, I'm new to .NET 2003 compiler. When I tried to compile my program using DEBUG mode, I got the following errors in the C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7 \include\xdebug file as folows. I need help to resolve them ASAP: cl /c /nologo /MDd /W3 /Od /GR /GM /Zi /GX /D "_DEBUG" /D " WIN32" /D "_W INDOWS" /D "_WINDLL" /D "_AFXDLL" /D "_MBCS" /D "_USRDLL" /
11
2598
by: christopher diggins | last post by:
I am wondering if any can point me to any open-source library with program objects for C++ like there is in Java? I would like to be able to write things like MyProgram1 >> MyProgram2 >> Fork(MyProgram3, SomeFile); If not would this be something of interest to others? Thanks in advance,
1
3261
by: Eric Whittaker | last post by:
hi all, im trying to write my first c++ program. a success, but i can't get the window to stay open after user enters input. it just automatically closes. right now the end of my program looks like this: return 0; }
9
4526
by: Hemal | last post by:
Hi All, I need to know the memory required by a c program. Is there any tool/utility which can give me the memory usage in terms of DATA segment, TEXT segment, BSS segment etc. I am working on linux platform and my target is ARM processor. But i guess it should not matter. Actually i need to know both RAM & ROM usage.
7
13263
by: ibtc209 | last post by:
I just started programming in C, and I need some help with this problem. Your program will read the information about one MiniPoker hand, namely the rank and suit of the hand’s first card, and the rank and suit of its second card. Note that the two cards in a hand may be entered in any order; it’s not necessarily the case that the highest card will appear first, for example. Your program will then determine whether the hand is valid, and...
2
19338
Banfa
by: Banfa | last post by:
Posted by Banfa The previous tutorial discussed what programming is, what we are trying to achieve, the answer being a list of instructions constituting a valid program. Now we will discuss how we set about doing that. Every program starts with a specification, this may be a several hundred page document from your latest client or one small paragraph from your professor and pretty much anything in-between. The specification is very...
0
13327
amitpatel66
by: amitpatel66 | last post by:
There is always a requirement that in Oracle Applications, the Concurrent Program need to be execute programatically based on certain conditions/validations: Concurrent programs can be executed programatically either from UNIX or Oracle PLSQL. In this Section, I will be explaining about calling a Concurrent program from UNIX using the CONCSUB Command. Pre-requisite: 1. Concurrent Program should be registered in oracle Applications...
0
8381
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8893
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8797
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8583
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8656
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5681
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4380
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2791
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2023
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.