Pseudo code to C code

Is anyone good at pseudo-code?
I don't really get how to write this into C code.
I did the first half and the last thought but i dont quite get the conditions in the middle.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<stdio.h>
#define TRUE 1
#define FALSE 0
#define NUMEL 10

int main()
{ int nums[NUMEL] = {5, 10, 22, 32, 45, 67, 73, 98, 99, 101 };
  int item, location;

  printf("Enter the item you are searching for: ");
  scanf("%d", &item);

/* Set a "found" flag to FALSE
   Set an index value to -1
   Begin with the first item in the list
   While there are still items in the list AND the "found" flag is FALSE
     Compare the item with the desired item
     If the item was found
           set the "found" flag to TRUE
           Set the index value to the item's position in the list.
     ENDIF
   ENDWHILE
   location <- index
*/

if(location>-1)
   printf("The item was found at index location %d\n", location);
else
   printf("The item was not found in the list\n");
return o;
}

 
Last edited on
I may not know C as well as I know C++, but this might help

Firstly, it'll be a little different depending on whether or not you are using C99 or an earlier standard.

For C99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdbool.h> //this gives us access to bool
//skip the code you've already written
bool found = FALSE;
int index = -1;
//if you want to iterate through the array you've made, you must start at index 0, not -1.
//i'm not sure what you want to do with the index variable set to -1.
//if you try to iterate with an index of -1, you will get an out-of-bounds issue
int arrayIndex = 0;
while (arrayIndex <= 9 && found == FALSE)
{
     if (item == nums[arrayIndex]
          {
               found = TRUE;
               //arrayIndex will tell us which index it is at
               break; //not necessary, as the while loop's condition will fail and the program
               // will exit the while loop, but this might help visualize what is going on
          }
     else
          arrayIndex = arrayIndex + 1;
}
location = arrayIndex; //this will be 0 if the first element in the array was the number that
//the user was looking for, so add 1 to this if you want it to show 1 as being the first element
//continue with the rest of your code 


so, if the user wanted to look for 10, that would be the second element in the array. arrayIndex will equal 1 (since we count from 0 not from 1). You can just add 1 to arrayIndex if you want the user to see "item found at location 2" instead of "item found at location 1".

You could also change the end of your code to this:
1
2
3
4
if (found == TRUE)
     printf("The item was found at index location %d\n", location);
else
     printf("The item was not found in the list\n");


If you're not using C99 and don't have access to <stdbool.h>, then replace 'bool' with 'int', and use 0 for FALSE and 1 for TRUE.

Like I said, I don't know C as well as I know C++, so you might want to double-check my syntax for errors. I hope this helps.
Thanks a bunch! I think i can translate it into C++
I get the idea now. Thanks a lot!!
Topic archived. No new replies allowed.