GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-Jun-2008, 01:14
phoenix69 phoenix69 is offline
New Member
 
Join Date: May 2008
Posts: 12
phoenix69 is on a distinguished road
Question

Array problem !!


I can't make it to search and display the input number. My program gives me wrong output. Can anybody help me ? Thanks.
CPP / C++ / C Code:
/**Five numbers are entered from the keyboard into an array. 
The number to be searched is entered through the keyboard by the user. 
Write a program to find if the number to be searched is present in the array and 
if it is present, display the number of times it appears in the array.**/

#include<stdio.h>

int main()
{
int num[5];
int i, s;
 
printf("Enter 5 int numbers ");
for (i=0;i<=4;i++){
scanf("%d",&num[i]);
}

s=0;
printf("\nEnter a number to search " );
scanf("%d",&s);

for (i=0;i<=4;i++){
if(num[i]==s)
s++;

}
printf("\nThere were %d numbers ",s);

return 0;
}
  #2  
Old 13-Jun-2008, 06:05
mamntc02 mamntc02 is offline
Junior Member
 
Join Date: Mar 2008
Location: Barcelona - Catalonia
Posts: 63
mamntc02 has a spectacular aura aboutmamntc02 has a spectacular aura about

Re: Array problem !!


Hi,

your mistake is:
CPP / C++ / C Code:
for (i=0;i<=4;i++){
	if(num[i]==s)
		s++;
}
Why do you increase the variable (s)that has the number to be searched? You need another variable (a counter) to count how many times a number is found.
CPP / C++ / C Code:
int count = 0;
for (i=0;i<=4;i++){
	if(num[i]==s)
		count++;
}
Regards
__________________
Please, correct me if I'm wrong, and sorry for my english ;)
  #3  
Old 13-Jun-2008, 06:15
phoenix69 phoenix69 is offline
New Member
 
Join Date: May 2008
Posts: 12
phoenix69 is on a distinguished road

Re: Array problem !!


Thank you very much. I've got it.
 
 

Recent GIDBlogMatch IP in CIDR by gidnetwork

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
where is the problem and can you fix it (php) oggie MySQL / PHP Forum 8 14-Apr-2008 15:08
Getting a line error in register oggie MySQL / PHP Forum 5 13-Apr-2008 16:16
Returning a 2 dimensional Array from a function vicky_brsh C++ Forum 1 04-Jan-2008 14:06
Loop problem on an array trp_be C Programming Language 1 06-Jan-2007 07:49
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 19:31

Network Sites: GIDNetwork · GIDApp · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:27.


vBulletin, Copyright © 2000 - 2013, Jelsoft Enterprises Ltd.