Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to write a program by using array?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
write a function take an array as number and scan the array to find this number?
 
can you be a bit more spacific "take an array as number"??
what kind of array have you got (int, float ,char?) is it a 1 dimensional or multi?.
 
hi, denster thank you feedback to me,
actually i want to learn write program using C languages, and for this question, i already wrote it but dun know whether it correct or not. this function is accept int, and we need to write a function to scan the number and find the number that we required.
** just a function**

enum Boolean{false=0, true=1);
int key_array[25]
{
int key_to_found, n;
int count =1;
boolean=false;
Repeat
if (key_to_found == key_array[count]
Boolean=true;
else
{ count=count+1;}
until
Boolean = true || count=key_Array[25];
}

can i write like that?
------------------------------------------------------------

and i have some more question need your advise.
if a program accept 2 string and then swap the string, can i write the function like below:

void swap(char *x, char *y)
{
char temp;
temp=*x;
*x=*y;
*y=temp;
printf("x=%s y=%s\n", *x, *y);
}
------------------------------------------------------------

what is the adv of using structure? and normally how we create structure?
------------------------------------------------------------
lastly, what is the problem if record manipulate in array only? we should use pointer when we declare array right?

thank for your help.

















 
Hi chintseming: Here is a C program that will 1: swap 2 strings and 2: search an int array for a value that you specify ->

#include <stdio.h>
#include <string.h>

#define MAX_STRING 50
#define NUM_OF_ELEMS(array) (sizeof(array)/sizeof(array[0]))


void Swap_Strings( char *str1,char *str2 );
int Array_Search( int array[],int iSearchNum,
int iSizeOfArray );


int main() {

char sStringOne[] = &quot;This is string one!!!&quot;;
char sStringTwo[] = &quot;This is string two!!!&quot;;
int iArrayToSearch[] = { 2,5,4,6,8,5,10 };
int iInput = 0;
int iResult = 0;


printf( &quot;Before Swap_Strings()\n&quot; );
printf( &quot;sStringOne = %s\n&quot;,sStringOne );
printf( &quot;sStringTwo = %s\n&quot;,sStringTwo );

Swap_Strings( sStringOne,sStringTwo );

printf( &quot;After Swap_Strings()\n&quot; );
printf( &quot;sStringOne = %s\n&quot;,sStringOne );
printf( &quot;sStringTwo = %s\n&quot;,sStringTwo );


printf( &quot;Enter a number to search for: &quot; );
scanf( &quot;%d&quot;,&iInput );

iResult = Array_Search( iArrayToSearch,iInput,
NUM_OF_ELEMS(iArrayToSearch));

if ( iResult != -1 )
printf( &quot;The number:%d was found at elem. %d\n\n&quot;,
iInput,iResult );
else
printf( &quot;The number %d was not found.\n\n&quot;,iInput );

return 0;

}

int Array_Search( int array[],int iSearchNum,
int iSizeOfArray ) {

for (int i=0;i<iSizeOfArray;i++ )
{

if ( array == iSearchNum )
return i;

}


return -1;

}


void Swap_Strings( char *str1,char *str2 ) {

char sTempString[MAX_STRING];

strcpy( sTempString,str1 );
strcpy( str1,str2 );
strcpy( str2,sTempString );

}

I hope this will help you in your studies. This is just a simple overview of one way you could implement this. You might also want to put some error checking code into the swap function to see if str1 is big enough to hold str2 and vice versa.

LiquidBinary.



mlg400@blazemail.com
 
Also: Structures are always nice. They provide a very convient way to organize your data. Think of them as creating your own little types. The normal format of a structure is as follows ->

struct person {

char sName[MAX_SIZE];
int iAge;
float fHeight;

};

Then you can define your person structure ->

person someGuy;

someGuy.iAge = 35;
someGuy.fHeight = 5.8;
strcpy( someGuy.sName,&quot;Linus Torvalds&quot; );

etc...you get the picture. When you get into C++,you'll get to work with a more advanced type of structure called a class. In C++ the only difference between a struct and a class is the attributes(member variables)of a class are private by default and you have to create methods to access them outside of the class. With struct's, the attributes are public by default. Theres alot of literature on the web that will give you a better understanding of all of this.

LiquidBinary. mlg400@blazemail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top