All About Circuits Forum  

Go Back   All About Circuits Forum > Software, Microcomputing, and Communications Forums > Programmer's Corner

Notices

Programmer's Corner Discussion forum for all aspects of programming and software engineering. Any software programming language welcome: C, C++, C#, Fortran, Java, Matlab, etc.

Reply   Post New Thread
 
Thread Tools Display Modes
  #11  
Old 02-10-2010, 12:35 AM
indianhits indianhits is offline
Junior Member
 
Join Date: Jul 2009
Posts: 45
Default

both are same even if you put curly brackets or not
Reply With Quote
  #12  
Old 02-10-2010, 05:47 AM
count_volta's Avatar
count_volta count_volta is offline
Senior Member
 
Join Date: Feb 2009
Location: Pennsylvania
Posts: 313
Send a message via MSN to count_volta
Default

Quote:
Originally Posted by mik3 View Post
int find is a subroutine and the return instruction ends the subroutine. Thus, it will not execute the return -1 if a[i]==x.
Okay I see, so if a[i] == x, it will return i and go back to main program, but if a[i] never equals to x, it will get to the end of the loop, jump out of the loop and return -1 and then go back to main program.

Cool thanks. Now I can do that in mips assembly pretty easily.
__________________
"The gift of mental power comes from God, if we concentrate our minds on that truth,we become in tune with this great power" Nikola Tesla

Science is nothing more than our feeble attempt to understand the mysteries of God. When we succeed, and claim to understand everything there is to know, we show our foolishness.

Thank you to all the people who worked on the All About Circuits ebook. It is one of the best electronics books out there.

I'm an EE student at the University of Pittsburgh
Reply With Quote
  #13  
Old 03-03-2010, 11:55 PM
Harrington Harrington is offline
Banned
 
Join Date: Dec 2009
Location: London
Posts: 86
Default C programming

Hi this should give you a better idea of how this works

I've rewritten the code for you so that it makes more sense then you can see what it does and hopefully get a better understanding

#include <stdio.h>


// declare global functions

int find( int a[], int n, int x) ;

int Array[] = { 1,3,5,4,10} ;


// define the function in full here


int find( int a[], int n, int x)
{
int i = -1 ;

for (i=0; i!=n; i++)
{

if(a[i]==x){

printf("Match found %d at index X = %d \n \n ",i,x);

return i;


} // end if

}// end for


if (i == -1 )
{
printf("\n No Match found \n") ;

}

return i;



}// end function






void main()
{


char ch =' ';

int x = 0;

int n = 0 ;

int match = 0 ;

int j = 0 ;

int flag = 1 ;


printf("Enter q to quit or r to run \n\n");


while(flag ==1){


ch = getchar(); // get user input


if(ch == 'r')
{
printf(" Enter a value for x please followed by enter\n ") ; // prompt the user for input

scanf ("%d",&n);

printf("Enter a value for n please in the range of 0 to 5 followed by return\n");

scanf ("%d",&x); // get user input

match = find(Array,n,x); // simply return our value here



printf("\n Array contents were \n");
for( j = 0 ; j < 5 ; j ++)

{

printf(" %d " ,Array[j]);
}




printf("\n\n q to quit or r to run\n\n");

}

if (ch =='q')
{

flag = 0 ;

}



} // end while


printf("\n User typed q \n **** Quitting program **** \n" );




}// end main method
Reply With Quote
Reply   Post New Thread

Bookmarks

Tags
, ,

Thread Tools
Display Modes

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

BB 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
Parallel Wiring Very Simple Question turzel09 General Electronics Chat 5 06-22-2009 07:30 PM
simple Thevinin question Musab Homework Help 7 05-30-2009 08:48 PM
A simple question for confirmation FND General Electronics Chat 11 04-03-2009 09:02 PM
A simple question about diodes. farmosh203 Homework Help 5 01-29-2008 05:27 AM
Very simple question, voltage divider Alvin General Electronics Chat 16 01-24-2008 05:10 PM


All times are GMT. The time now is 10:30 PM.


User-posted content, unless source quoted, is licensed under a Creative Commons Public Domain License. Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.