CODE
/* Filename: C19SERCH.C
Searches a part number array for the input value. If
the entered part number is not in the array, it is
added. If the part number is in the array, a message
is printed. */
#include <stdio.h>
#define MAX 100
int main(void);
int fillParts(long int parts[MAX]);
int main(void)
{
long int searchPart; /* Holds user request */
long int parts[MAX];
int ctr;
int numParts=5; /* Beginning inventory count */
fillParts(parts); /* Fills the first five elements */
do
{
printf("\n\nPlease type a part number...");
printf("(-9999 ends program) ");
scanf("%ld", &searchPart);
if (searchPart == -9999)
{ break; } /* Exits loop if user wants */
/* Scan array to see whether part is in inventory */
for (ctr=0; ctr<numParts; ctr++) /* Checks each item */
{ if (searchPart == parts[ctr]) /* If it is in
inventory...*/
{ printf("\nPart %ld is already in inventory",
searchPart);
break;
}
else
{ if (ctr == (numParts-1) ) /* If not there,
adds it */
{ parts[numParts] = searchPart; /* Adds to
end of array */
numParts++;
printf("\n%ld was added to inventory\n",
searchPart);
break;
}
}
}
} while (searchPart != -9999); /* Loops until user
signals end */
return 0;
}
int fillParts(long int parts[MAX])
{
/* Assigns five part numbers to array for testing */
parts[0] = 12345;
parts[1] = 24724;
parts[2] = 54154;
parts[3] = 73496;
parts[4] = 83925;
return 0;
}
Searches a part number array for the input value. If
the entered part number is not in the array, it is
added. If the part number is in the array, a message
is printed. */
#include <stdio.h>
#define MAX 100
int main(void);
int fillParts(long int parts[MAX]);
int main(void)
{
long int searchPart; /* Holds user request */
long int parts[MAX];
int ctr;
int numParts=5; /* Beginning inventory count */
fillParts(parts); /* Fills the first five elements */
do
{
printf("\n\nPlease type a part number...");
printf("(-9999 ends program) ");
scanf("%ld", &searchPart);
if (searchPart == -9999)
{ break; } /* Exits loop if user wants */
/* Scan array to see whether part is in inventory */
for (ctr=0; ctr<numParts; ctr++) /* Checks each item */
{ if (searchPart == parts[ctr]) /* If it is in
inventory...*/
{ printf("\nPart %ld is already in inventory",
searchPart);
break;
}
else
{ if (ctr == (numParts-1) ) /* If not there,
adds it */
{ parts[numParts] = searchPart; /* Adds to
end of array */
numParts++;
printf("\n%ld was added to inventory\n",
searchPart);
break;
}
}
}
} while (searchPart != -9999); /* Loops until user
signals end */
return 0;
}
int fillParts(long int parts[MAX])
{
/* Assigns five part numbers to array for testing */
parts[0] = 12345;
parts[1] = 24724;
parts[2] = 54154;
parts[3] = 73496;
parts[4] = 83925;
return 0;
}
Здравейте, първо искам да попитам в тази част:
Код:
CODE
for (ctr=0; ctr<numParts; ctr++) /* Checks each item */
{ if (searchPart == parts[ctr]) /* If it is in
inventory...*/
{ printf("\nPart %ld is already in inventory",
searchPart);
{ if (searchPart == parts[ctr]) /* If it is in
inventory...*/
{ printf("\nPart %ld is already in inventory",
searchPart);
, по какъв начин се проверява дали някоя от стойностите съвпада с тези от масива? Код:
CODE
searchPart == parts[ctr]
- този ред не проверява ли за текущата стойност на 'ctr'? И последния ми въпрос е как тази проверка установява дали числото съществува:Код:
CODE
if (ctr == (numParts-1) )
?
