i need to find the max position of my program. how can i do that? -
int main()
-
{
-
int j[10],max=0,k;
-
for(k=0;k<=9;k++)
-
{
-
printf("Enter the number:");
-
scanf("%d", &j[k]);
-
}
-
for(k=0;k<=9;k++)
-
{
-
printf("x[%d]=%d\n", k, j[k]);
-
if(max<j[k])
-
{
-
max=j[k];
-
}
-
}
-
printf("The maximum number is %d\n",max);
-
}
-
6 17616
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. -
for(k=0;k<=9;k++)
-
{
-
if(max<j[k])
-
{
-
max=j[k];
-
iMaxPosition = k;
-
}
-
}
-
Regards
Dheeraj Joshi
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. - #include <stdlib.h>
-
#include<iostream>
-
#include<string>
-
using namespace std;
-
-
int main()
-
char a[30];
-
string s1;
-
int b[5];
-
cout<<"Enter desired string\n";
-
cin.getline(a,30);
-
s1=a;
-
int n=0,i;
-
for(i=0;i<30;i++)
-
{
-
if(a[i]==' '&& a[i+1]==' ')
-
{
-
n++;
-
b[i]=i;
-
}
-
-
}
-
cout<<"The white spaces in given string are "<<n;
-
cout<<"\n";
-
i=0;
-
while(i<n)
-
{
-
s1.erase(b[i],1);
-
i++;
-
}
-
-
-
cout<<"\nThe new formatted string is \n";
-
cout<<s1;
-
return 0;
-
}
-
Any suggestions,hints?
Thanks in advance.
Cheers.
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.
This loop is guaranteed to run over the bounds of a: - for(i=0;i<30;i++)
-
{
-
//when i = 29, (i+1) = 30!
-
if(a[i]==' '&& a[i+1]==' ')
-
{
-
n++;
-
b[i]=i;
-
}
-
-
}
Also, I belive you should to use
instead of
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.
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.
@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();
}
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
...
|
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...
|
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 >>...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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...
|
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: 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: 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...
| |