arrays - Validating a Barcode in C -
i beginner c coder working on program validate bar codes. far have code reads in 12 digit bar code. however, having trouble on attaching 12 digit input functions odd_value , even_value. need theses values validate bar code.
#include <stdio.h> #define array_size 12 int print_values(int* x) { printf("\nstep 1: sum of odds times 3 \n" ); printf("step 2: sum of digits \n"); printf("step 3: total sum \n" ); printf("step 4: calculated check digit \n" ); printf("step 5: check digit , last digit \n" ); return 0; } int odd_value() { int sum_odd = (x[0] + x[2] + x[4] + x[6] + x[8] + x[10])*3; return sum_odd; } int even_value() { int sum_even = (x[1] + x[3] + x[5] + x[7] + x[9] + x[11]); return sum_even; } int fill_array(){ int i; printf("enter barcode check. separate digits space >\n"); for(i=0; i<array_size; i++){ if(scanf("%d:", &x[i]) != 1) return 0; } return 1; } int main(){ int bar_code[array_size]; int i, last_digit, check_digit, odd, even; if(fill_array(bar_code)) { printf("\nyou entered code: "); for(i=0; i<array_size; i++) printf("%d ", bar_code[i]); } else { puts("failed read"); } odd = odd_value(); = even_value(); last_digit = odd + even; if(last_digit == 0) { check_digit = 0; } else { check_digit = 10 - last_digit; } print_values(bar_code); if(check_digit == bar_code[array_size]) { printf("barcode valid\n"); } else { printf("barcode invalid\n"); } return 0; }
edit: updated code little bit bit confused. still working on it, there few main errors getting.
first error: function 'print_values' line 5: 'x' undeclared (first use in function) (each undeclared identifier reported once)
second error: function 'main' line 61: expected expression before 'const'
#include <stdio.h> #define array_size 12 void print_values(int odd_value(const int* x), int even_value(const int* x), int last_digit) { printf("\nstep 1: sum of odds times 3 %d\n", odd_value(&x) ); printf("step 2: sum of digits %d\n", even_value(&x)); printf("step 3: total sum %d\n", last_digit); printf("step 4: calculated check digit \n" ); printf("step 5: check digit , last digit \n" ); } int odd_value(const int* x) { int sum_odd = (x[0] + x[2] + x[4] + x[6] + x[8] + x[10])*3; return sum_odd; } int even_value(const int* x) { int sum_even = (x[1] + x[3] + x[5] + x[7] + x[9] + x[11]); return sum_even; } int fill_array(int* x){ int i; printf("enter barcode check. separate digits space >\n"); for(i=0; i<array_size; i++){ if(scanf("%d:", &x[i]) != 1) return 0; } return 1; } int main(){ int bar_code[array_size]; int i, last_digit, check_digit, odd, even; if(fill_array(bar_code)) { printf("\nyou entered code: "); for(i=0; i<array_size; i++) printf("%d ", bar_code[i]); } else { puts("failed read"); } odd = odd_value(bar_code); = even_value(bar_code); last_digit = odd + even; if(last_digit == 0) { check_digit = 0; } else { check_digit = 10 - last_digit; } if(check_digit == bar_code[11]) { printf("barcode valid\n"); } else { printf("barcode invalid\n"); } print_values(odd_value(const int* x), even_value(const int* x), last_digit); return 0; }
edit:
i believe have 1 last question code. need last_digit equal last_digit in value sum. in main function have last_digit = sum, need manipulate last_digit in value sum. example, if sum = 72, last_digit 2.
#include <stdio.h> #define array_size 12 void print_values(int odd, int even, int sum, int last_digit, int check_digit) { printf("\nstep 1: sum of odds times 3 %d\n", odd ); printf("step 2: sum of digits %d\n", even); printf("step 3: total sum %d\n", sum); printf("step 4: calculated check digit %d\n", check_digit); if(check_digit == last_digit) { printf("step 5: check digit , last digit match\n" ); } else { printf("step 5: check digit , last digit not match\n"); } } int odd_value(const int* x) { int sum_odd = (x[0] + x[2] + x[4] + x[6] + x[8] + x[10])*3; return sum_odd; } int even_value(const int* x) { int sum_even = (x[1] + x[3] + x[5] + x[7] + x[9]); return sum_even; } int fill_array(int* x){ int i; printf("enter barcode check. separate digits space >\n"); for(i=0; i<array_size; i++){ if(scanf("%d:", &x[i]) != 1) return 0; } return 1; } int main(){ int bar_code[array_size]; int i, last_digit, check_digit, odd, even; if(fill_array(bar_code)) { printf("\nyou entered code: "); for(i=0; i<array_size; i++) printf("%d ", bar_code[i]); } else { puts("failed read"); } odd = odd_value(bar_code); = even_value(bar_code); int sum = odd + even; last_digit = sum; if(last_digit == 0) { check_digit = 0; } else { check_digit = 10 - last_digit; } print_values(odd, even, sum, last_digit, check_digit); if(check_digit == last_digit) { printf("barcode valid\n"); } else { printf("barcode invalid\n"); } return 0; }
create argument pass (pointer first element of) array , use it.
int odd_value(const int *x) { int sum_odd = (x[0] + x[2] + x[4] + x[6] + x[8] + x[10])*3; return sum_odd; } int even_value(const int *x) { int sum_even = (x[1] + x[3] + x[5] + x[7] + x[9] + x[11]); return sum_even; }
calling them should this:
odd = odd_value(bar_code); = even_value(bar_code);
note definition of fill_array()
wrong , first line of should int fill_array(int *x){
.
update: new code:
- define
x
inprint_values()
, or cannot use in function. - your arguments calling
print_values()
invalid. write function name (identifier) pass function pointers.
i corrected arguments odd_value()
, even_value()
in print_values()
have them suit added x
.
improved code:
#include <stdio.h> #define array_size 12 void print_values(const int *x, int odd_value(const int* x), int even_value(const int* x), int last_digit) { printf("\nstep 1: sum of odds times 3 %d\n", odd_value(x)); printf("step 2: sum of digits %d\n", even_value(x)); printf("step 3: total sum %d\n", last_digit); printf("step 4: calculated check digit \n" ); printf("step 5: check digit , last digit \n" ); } int odd_value(const int* x) { int sum_odd = (x[0] + x[2] + x[4] + x[6] + x[8] + x[10])*3; return sum_odd; } int even_value(const int* x) { int sum_even = (x[1] + x[3] + x[5] + x[7] + x[9] + x[11]); return sum_even; } int fill_array(int* x){ int i; printf("enter barcode check. separate digits space >\n"); for(i=0; i<array_size; i++){ if(scanf("%d:", &x[i]) != 1) return 0; } return 1; } int main(void){ int bar_code[array_size]; int i, last_digit, check_digit, odd, even; if(fill_array(bar_code)) { printf("\nyou entered code: "); for(i=0; i<array_size; i++) printf("%d ", bar_code[i]); } else { puts("failed read"); } odd = odd_value(bar_code); = even_value(bar_code); last_digit = odd + even; if(last_digit == 0) { check_digit = 0; } else { check_digit = 10 - last_digit; } if(check_digit == bar_code[array_size - 1]) { printf("barcode valid\n"); } else { printf("barcode invalid\n"); } print_values(bar_code, odd_value, even_value, last_digit); return 0; }
Comments
Post a Comment