Related
I have an assignment for the Cache Simulator. every time I run code it gives error1 that is used in the if statement. I am trying to open the input file but it didn't open and gives error1. I tried to catch this error but failed.
I am posting my whole code because I don't want to make assumptions about where the problem may lie. Can anyone tell where I am making a mistake?
Thanks
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#include<math.h>
struct cacheBlock{
unsigned int valid;
unsigned long int tag;
unsigned int time;
struct cacheBlock *next;
};
int memoryReadsNoPrefetch = 0;
int memoryWritesNoPrefetch = 0;
int cacheHitsNoPrefetch = 0;
int cacheMissesNoPrefetch = 0;
int memoryReadsPrefetch = 0;
//int memoryWritesPrefetch = 0;
int cacheHitsPrefetch = 0;
int cacheMissesPrefetch = 0;
int powerOfTwo(int num){
if(num==0){
return 0;
}
while(num!=1){
if(num%2!=0){
return 0;
}
num=num/2;
}
return 1;
}
struct cacheBlock** allocateCache(int sets, int n){
//printf("Aa\n");
struct cacheBlock** temp = malloc(sizeof(struct cacheBlock) * sets);
//printf("Ab\n");
for (int i = 0; i < sets; i++){
//printf("Ac\n");
temp[i] = malloc(sizeof(struct cacheBlock) * n);
//printf("Ad\n");
for (int j = 0; j < n; j++) {
//printf("Ae\n");
temp[i][j].valid=0;
//printf("Af\n");
}
//printf("Ag\n");
}
//printf("Ah\n");
return temp;
}
void readNoPrefetch(struct cacheBlock** cache, unsigned long tag, int index, int sets, int n){
int flag=0;
for(int i=0; i < n; i++){
if(cache[index][i].valid==1 && cache[index][i].tag==tag){
flag=1;
cacheHitsNoPrefetch++;
break;
}
}
if(flag==1){
return;
}else{
cacheMissesNoPrefetch++;
memoryReadsNoPrefetch++;
int full = 1;
for(int i=0; i<n; i++){
if(cache[index][i].valid==1){
cache[index][i].time++;
}
if(cache[index][i].valid==0){
full = 0;
break;
}
}
int max=0;
if(full == 1){
for(int i=0; i<n; i++){
if(cache[index][i].time > max){
max = cache[index][i].time;
}
}
for(int j=0; j<n; j++){
if(cache[index][j].time==max){
cache[index][j].valid=0;
cache[index][j].tag=(-1);
cache[index][j].time=0;
break;
}
}
}
for(int i=0; i < n; i++){
if(cache[index][i].valid==0){
cache[index][i].valid=1;
cache[index][i].tag=tag;
}
}
}
}
void writeNoPrefetch(struct cacheBlock** cache, unsigned long tag, int index, int sets, int n){
int flag = 0;
for(int i = 0; i < n; i++){
if(cache[index][i].valid==1 && cache[index][i].tag == tag){
flag = 1;
cacheHitsNoPrefetch++;
memoryWritesNoPrefetch++;
break;
}
}
if(flag==1){
return;
}else{
readNoPrefetch(cache, tag, index, sets, n);
memoryWritesNoPrefetch++;
return;
}
}
void prefetch(struct cacheBlock** cache, unsigned long int address, int prefetch_size, int n, int blockSize, int setBits, int offsetBits){
int flag=0;
unsigned long int prefetch_address = address;
for (int i = 0; i < prefetch_size; i++){
prefetch_address = prefetch_address + blockSize;
int Mask = (1 << setBits) - 1;
unsigned int set_index = (prefetch_address >> offsetBits) & Mask;
unsigned long tag = (prefetch_address >> offsetBits) >> setBits;
for(int j = 0; j < n; j++){
if(cache[set_index][j].valid==1 && cache[set_index][j].tag==tag){
flag=1;
break;
}
}
if(flag==1){
return;
}else{
memoryReadsPrefetch++;
int full = 1;
for(int i=0; i<n; i++){
if(cache[set_index][i].valid==1){
cache[set_index][i].time++;
}
if(cache[set_index][i].valid==0){
full = 0;
break;
}
}
int max=0;
if(full == 1){
for(int i=0; i<n; i++){
if(cache[set_index][i].time > max){
max = cache[set_index][i].time;
}
}
for(int j=0; j<n; j++){
if(cache[set_index][j].time==max){
cache[set_index][j].valid=0;
cache[set_index][j].tag=(-1);
cache[set_index][j].time=0;
break;
}
}
}
for(int i=0; i < n; i++){
if(cache[set_index][i].valid==0){
cache[set_index][i].valid=1;
cache[set_index][i].tag=tag;
// memoryReadsPrefetch++;
}
}
}
}
}
void readPrefetch(struct cacheBlock** cache, unsigned long tag, int index, int sets, int n, int prefetch_size, int blockSize, int offsetBits, int setBits, unsigned long int address){
int flag=0;
for(int i=0; i < n; i++){
if(cache[index][i].valid==1 && cache[index][i].tag==tag){
flag=1;
cacheHitsPrefetch++;
break;
}
}
if(flag==1){
//cacheHitsPrefetch++;
return;
}else{
cacheMissesPrefetch++;
memoryReadsPrefetch++;
int full = 1;
for(int i=0; i<n; i++){
if(cache[index][i].valid==1){
cache[index][i].time++;
}
if(cache[index][i].valid==0){
full = 0;
break;
}
}
int max=0;
if(full == 1){
for(int i=0; i<n; i++){
if(cache[index][i].time > max){
max = cache[index][i].time;
}
}
for(int j=0; j<n; j++){
if(cache[index][j].time==max){
cache[index][j].valid=0;
cache[index][j].tag=(-1);
cache[index][j].time=0;
break;
}
}
}
for(int i=0; i < n; i++){
if(cache[index][i].valid==0){
cache[index][i].valid=1;
cache[index][i].tag=tag;
}
}
prefetch(cache, address, prefetch_size, n, blockSize, setBits, offsetBits);
}
}
void writePrefetch(struct cacheBlock** cache, unsigned long tag, int index, int sets, int n, int prefetch_size, int blockSize, int offsetBits, int setBits, unsigned long int address){
int flag = 0;
for(int i = 0; i < n; i++){
if(cache[index][i].valid==1 && cache[index][i].tag == tag){
flag = 1;
cacheHitsPrefetch++;
break;
}
}
if(flag==1){
//cacheHitsPrefetch++;
return;
}else{
readPrefetch(cache, tag, index, sets, n, prefetch_size, blockSize, offsetBits, setBits, address);
return;
}
}
int main(int argc, char** argv){
//error checks
if (argc != 7){
printf("error1\n");
exit(0);
}
FILE* fp = fopen(argv[6], "r");
if(fp == NULL){
printf("error2\n");
exit(0);
}
int cacheSize = atoi(argv[1]);
int blockSize = atoi(argv[2]);
int prefetchSize = atoi(argv[5]);
int n = 0;
int setNumber = 0;
if(powerOfTwo(cacheSize)==0 || powerOfTwo(blockSize==0)){
printf("error3\n");
return 0;
}
/*if(strcmp(argv[4], "fifo")!=0 || strcmp(argv[4], "lru")!=0){
printf("error\n");
return 0;
}else if(strcmp(argv[4], "lru")==0){
return 0;
}*/
if(strcmp(argv[4], "direct")==0){
n = 1;
setNumber = cacheSize/blockSize;
}else if(strcmp(argv[4], "assoc")==0){
n = cacheSize/blockSize;
setNumber = 1;
}else if(strncmp(argv[4], "assoc:", 6)==0){
sscanf(argv[4], "assoc:%d", &n);
if(powerOfTwo(n)==0){
printf("error5\n");
return 0;
}
setNumber = cacheSize/(blockSize*n);
}else{
printf("error4\n");
return 0;
}
//all inputs are valid
struct cacheBlock** noPrefetchCache = allocateCache(setNumber, n);
struct cacheBlock** prefetchCache = allocateCache(setNumber, n);
int offsetBits = (int) (log(blockSize)/log(2));
int setBits = (int) (log(setNumber)/log(2));
unsigned long int address = 0;
char readOrWrite = ' ';
int Mask = (1 << setBits) - 1;
while((fscanf(fp, "%c %lx\n", &readOrWrite, &address)!=EOF && readOrWrite!='#')){
unsigned int set_index = (address >> offsetBits) & Mask;
unsigned long tag = (address >> offsetBits) >> setBits;
if(readOrWrite=='R'){
readNoPrefetch(noPrefetchCache, tag, set_index, setNumber, n);
readPrefetch(prefetchCache, tag, set_index, setNumber, n, prefetchSize, blockSize, offsetBits, setBits, address);
}else if(readOrWrite=='W'){
writeNoPrefetch(noPrefetchCache, tag, set_index, setNumber, n);
writePrefetch(prefetchCache, tag, set_index, setNumber, n, prefetchSize, blockSize, offsetBits, setBits, address);
}
}
fclose(fp);
printf("no-prefetch\n");
printf("Memory reads: %d\n", memoryReadsNoPrefetch);
printf("Memory writes: %d\n", memoryWritesNoPrefetch);
printf("Cache hits: %d\n", cacheHitsNoPrefetch);
printf("Cache misses: %d\n", cacheMissesNoPrefetch);
printf("with-prefetch\n");
printf("Memory reads: %d\n", memoryReadsPrefetch);
printf("Memory writes: %d\n", memoryWritesNoPrefetch);
printf("Cache hits: %d\n", cacheHitsPrefetch);
printf("Cache misses: %d\n", cacheMissesPrefetch);
}
I have created a program that takes in input "n" numbers that the user chooses and then prints the most repeated one, but I have a problem with passing the values between the functions so it gives me 0 as a result. How can I solve it?
void most_present_number(int array[]);
int read_numbers(int array[]);
int main() {
int array[400];
most_present_number(array);
return 0;
}
void most_present_number(int array[]){
read_numbers(array);
int i = 0;
int Max = 0;
int Current_number = vettore[0];
int Current_number_counter = 0;
int most_present_number = 0;
int most_present_number_counter = 0;
while (i < Max) {
if (array[i] == Current_number) {
Current_number_counter++;
i++;
} else {
if (Current_number_counter > most_present_number_counter){
most_present_number = Current_number;
most_present_number_counter = Current_number_counter;
}
Current_number = array[i];
Current_number_counter = 1;
i++;
}
}
printf("The most present number is %d which is repeated %d times\n", most_present_number,
most_present_number_counter);
}
int read_numbers(int array[]){
int Max = 0;
int i = 0;
printf("Insert the array lenght\n");
scanf("%d", &Max);
while (i < Max) {
printf("Insert the numbers\n");
scanf("%d", &array[i]);
i++;
}
return Max;
}
You have Max = 0 in most_present_number(), so the while loop stops immediately.
read_numbers() returns Max, so you can use this to initialize Max in most_present_number().
void most_present_number(int array[], int Max);
int read_numbers(int array[]);
int main() {
int array[400];
int size;
most_present_number(array);
return 0;
}
void most_present_number(int array[]){
int Max = read_numbers(array);
int i;
int Current_number = array[0];
int Current_number_counter = 0;
int most_present_number = 0;
int most_present_number_counter = 0;
for (i = 0; i < Max; i++) {
if (array[i] == Current_number) {
Current_number_counter++;
} else {
if (Current_number_counter > most_present_number_counter){
most_present_number = Current_number;
most_present_number_counter = Current_number_counter;
}
Current_number = array[i];
Current_number_counter = 1;
}
}
printf("The most present number is %d which is repeated %d times\n", most_present_number,
most_present_number_counter);
}
int read_numbers(int array[]){
int Max = 0;
int i = 0;
printf("Insert the array lenght\n");
scanf("%d", &Max);
while (i < Max) {
printf("Insert the numbers\n");
scanf("%d", &array[i]);
i++;
}
return Max;
}
Note also that your algorithm assumes that all the equal numbers will be together in the array. If they can be mixed up, you need a very different design. You need another array where you keep the counts of each number. Then at the end you find the entry in this array with the highest count.
This is a sample implementation of KD-tree.
Where I first take number of dimensions, number of points, number of clusters to be formed. Bi-partition function calculates centroid, dimension which has max variance. Now based on the max dimensions mean I start splitting the points. This program works fine when input is (dimensions-2,points-20,clusters-4). But does not work for (dimensions-2,points-20,clusters-8). When I debug the program it gives proper output.But when I run the program it stops working.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <stdbool.h>
int *gendata(int num);
void bipartition_fn(int dimensions,int nodes,int i0, int im, int *data,int *cluster_size,int *cluster_start, int *cluster_bdry, int *cluster_centroid);
void kdtree_fn(int dimensions, int nodes, int k, int *data,int *k_cluster_size,int **k_cluster_centroid,int *k_cluster_start,int **k_cluster_bdry );
int main()
{
int dimensions,nodes,i,k,j;
printf("enter the number of dimensions");
scanf("%d",&dimensions);
printf("enter the total number of elements in multiples of dimensions");
scanf("%d", &nodes);
printf("enter number of clusters");
scanf("%d",&k);
int *data;
int *k_cluster_size;
int **k_cluster_centroid;
int *k_cluster_start;
int **k_cluster_bdry;
data = gendata(nodes); /*dynamic array generation for data points*/
k_cluster_bdry=(int **)malloc(k*sizeof(int *));
for(i=0;i<(2*k-2);i++)
*(k_cluster_bdry+i)=(int *)malloc(2*dimensions*sizeof(int));
k_cluster_centroid=(int **)malloc(k*sizeof(int *));
for(i=0;i<(2*k-2);i++)
*(k_cluster_centroid+i)=(int *)malloc(dimensions*sizeof(int));
k_cluster_size=malloc((2*k-2)*sizeof(int));
k_cluster_start = malloc((2*k-2)*sizeof(int));
/*calling the kdtree function*/
kdtree_fn(dimensions, nodes, k, data, k_cluster_size, k_cluster_centroid, k_cluster_start, k_cluster_bdry);
/*printing the cluster size */
printf("cluster size \n");
for(i=k-2; i<(2*k - 2); i++){
printf("%d ", k_cluster_size[i]);
}
free(data);
free(k_cluster_bdry);
free(k_cluster_centroid);
free(k_cluster_size);
free(k_cluster_start);
return 0;
}
void kdtree_fn(int dimensions, int nodes, int k, int *data,int *k_cluster_size,int **k_cluster_centroid,int *k_cluster_start,int **k_cluster_bdry){
int i,j,d0,dm,x,m=0,l,n=0,check=1,s,temp=0;
d0 = 0, dm =nodes ;
int *cluster_size, *cluster_start;
int *cluster_bdry;
int *cluster_centroid;
int *query;
int *res;
query = (int *)malloc(sizeof(int)*dimensions);
res = (int *)malloc(sizeof(int)*dimensions);
cluster_centroid = (int*)malloc(dimensions*sizeof(int));
cluster_bdry = (int*)malloc(4*dimensions*sizeof(int));
cluster_size=(int*)malloc(2*sizeof(int));
cluster_start = (int*)malloc(2*sizeof(int));
/* iterating k-1 times to form k clusters */
for(x=0 ; x<k-1; x++){
bipartition_fn(dimensions, nodes, d0, dm, data, cluster_size, cluster_start, cluster_bdry, cluster_centroid);
for( i=0;i<dimensions; i++){
k_cluster_centroid[x][i] = cluster_centroid[i];
}
for( i=0;i<2; i++){
k_cluster_size[m] = cluster_size[i];
k_cluster_start[m] = cluster_start[i];
m++;
}
int p=0,r=0;
while(p<2){
l=0;
i=0;
while(i<2*dimensions){
k_cluster_bdry[temp][l] = cluster_bdry[r];
l++;
i++;
r++;
}
temp++;
p++;
}
s = pow(2,check);
if(x == 0 ||(x%(s-2)) == 0){
d0 =0;
nodes = k_cluster_size[n];
check++;
n++;
}
else{
d0 = d0+k_cluster_size[n-1];
nodes = k_cluster_size[n];
n++;
}
}
free(cluster_bdry);
free(cluster_centroid);
free(cluster_size);
free(cluster_start);
}
/*Each bipartition function gives 2 clusters*/
void bipartition_fn(int dimensions,int nodes,int d0, int dm, int *data,int *cluster_size,int *cluster_start, int *cluster_bdry, int *cluster_centroid){
int i,j,x,k;
int node = nodes/dimensions;
int sum,min,max;
int *cluster_assign;
cluster_assign = malloc(nodes*sizeof(int));
int *assign;
assign= (int *)malloc(node*sizeof(int));
// printf("nodes: %d \n", nodes);
/*calculate centroid and boundaries*/
i=0;
j=d0;
while(i<dimensions){
sum=0;
while(j<(d0+nodes)){
sum = sum+data[j];
j = j+dimensions;
}
cluster_centroid[i] = sum/node;
i = i+1;
j=d0+i;
}
/* Calculate variance of each dimension and find dimension with maximum variance*/
int var[dimensions],g,h;
h=d0;
g=0;
while(g<dimensions){
sum = 0;
while(h<(d0+nodes)){
sum = sum +((cluster_centroid[g] - data[h])*(cluster_centroid[g] - data[h]));
h=h+dimensions;
}
var[g] = sum/node;
g=g+1;
h=(d0+g);
}
int large = var[0];
int max_dimension =0;
int p;
for(p=0; p<dimensions; p++){
if(var[p]>large){
large = var[p];
max_dimension = p;
}
}
/* find mean of maximum variance*/
int mean = cluster_centroid[max_dimension];
//printf("mean %d \n",mean);
i=d0+max_dimension;
x=0;
while(i<(d0+nodes)){
if(data[i] < mean){
assign[x]=0;
}
else{
assign[x]=1;
}
x++;
i= i+dimensions;
}
/* Rearranging the points based on mean points lesser than mean goes to left and greater than mean goes to right*/
x=0;
int count=0;
int y=0;
for(i=0; i<node; i++){
if(assign[y] == 0){
count++;
for(j=dimensions*i; j<dimensions*(i+1); j++){
cluster_assign[x] = data[d0+j];
x++;
}
}
y++;
}
cluster_size[0] = count*dimensions;
cluster_start[0]= d0;
count=0;
y=0;
for(i=0; i<node; i++){
if(assign[y]!=0){
count++;
for(j=dimensions*i; j<dimensions*(i+1); j++){
cluster_assign[x] = data[d0+j];
x++;
}
}
y++;
}
cluster_size[1] = count*dimensions;
cluster_start[1]= d0+cluster_size[0];
int temp1,temp2;
x=0;
p=0;
while(p<2){
j=cluster_start[p];
i=0;
while(i<dimensions){
min=data[j];
max=data[j];
temp1=cluster_start[p];
temp2=cluster_size[p];
while(j < temp1+temp2){
if(data[j]<min)
min = data[j];
if(data[j]>max)
max= data[j];
j = j+dimensions;
}
cluster_bdry[x]=min;
x=x+1;
cluster_bdry[x]=max;
x=x+1;
i = i+1;
j=temp1+i;
}
p++;
}
/*printf("bou");
for(i=0; i<4*dimensions; i++){
printf("%d ",cluster_bdry[i]);
} */
free(cluster_assign);
free(assign);
}
/*Initialize data array*/
int *gendata(int num)
{
int *ptr = (int *)malloc(sizeof(int)*num);
int j = 0;
if(ptr != NULL)
{
for(j = 0; j < num; j++)
{
ptr[j] = -50 + rand()%101;
}
}
return ptr;
}
I know this is a very novice question but I am having trouble with my if and for() loops in this program. I tried to designate where the issues are, but basically in main() it asks if the user wants to dismiss an employee and then that employee ID. Then in the dismissWorker function, it's supposed to scan the array of worker ID's, find the ID that matches the employee to be fired (badID), and then change that array. In my dismissWorker function I have it printing "HI" just to see if I called it correctly in main (and it does), but it doesn't run through the for() loop. I'm sure it's something simple that I'm doing wrong, but what is it exactly? Also, I don't know what I should have it return to at the end of dismissWorker, so some suggestions would be helpful.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MAX_ROSTER 10 //roster of employees
typedef struct workerT_struct {
char name[81]; //employee name
char title[81]; //employee title
int empID; //unique ID for employee
int empStatus; //
int year100_salary; //before-tax salary, in cents
int year100_401k; //annual contribution to retirement, in cents
double taxRate; //fraction used to determine payroll taxes
int month100_paycheck; //monthly paycheck, in cents
} workerT;
typedef struct companyT_struct {
char dbaName[81]; //company name
int EmpActiveTot; //number of active employees
int EmpOnLeaveTot; //number of employees on unpaid leave
int year100_salaryTot; //total annual before-tax salaries, in cents
int year100_401kTot;
double year100_taxTot; //total annual payroll tax, in cents
int month100_paycheckTot; //total of all monthly paychecks for employees
} companyT;
void initWorkerArray(workerT list[], int siz);
void prntWorker(workerT list[], int siz, int indx);
void payWorker (workerT list[], int siz, int indx);
int dismissWorker (workerT list[], int siz, int badID);
void initCo(companyT hubCo[], workerT roll [], int sizRoll);
void doCoBooks(companyT hubCo[], workerT roll[], int sizRoll);
void printCoBooks(companyT hubCo[]);
int main()
{
workerT roster[MAX_ROSTER];
initWorkerArray(roster, MAX_ROSTER);
payWorker(roster, MAX_ROSTER, 0);
companyT myCo[1];
initCo(myCo, roster, 0);
doCoBooks(myCo, roster, MAX_ROSTER);
printCoBooks(myCo);
printf("Would you like to dismiss an existing employee? (yes/no)\n\n");
//FIXME FIXME FIXME
char defaultAnswer[4] = "yes";
char answer[4];
int badID = 0;
scanf(" %s", answer);
while (strcmp(answer,defaultAnswer) == 0) {
printf("Please give the employee ID:\n");
scanf(" %d", &badID);
printf("The employee ID you chose was %d.\n", badID);
dismissWorker(roster, MAX_ROSTER, 10); //FIXME
printf("Do you want to dismiss another employee?\n\n");
scanf(" %s", answer);
}
printf("Goodbye!");
return 0;
}
void initWorkerArray(workerT list[], int siz) {
int i = 0;
for (i = 0; i < 4; ++i) {
strcpy(list[0].name, "Buggy, Orson");
strcpy(list[0].title, "Director/President");
list[0].empID = 1;
list[0].empStatus = 1;
list[0].year100_salary = 12015000;
list[0].year100_401k = 950000;
list[0].month100_paycheck = 0;
list[0].taxRate = 0.45;
strcpy(list[1].name, "Czechs, Imelda");
strcpy(list[1].title, "Chief Financial Officer");
list[1].empID = 2;
list[1].empStatus = 1;
list[1].year100_salary = 8020000;
list[1].year100_401k = 960000;
list[1].month100_paycheck = 0;
list[1].taxRate = 0.39;
strcpy(list[2].name, "Hold, Levon");
strcpy(list[2].title, "Customer Service");
list[2].empID = 6;
list[2].empStatus = -1;
list[2].year100_salary = 8575000;
list[2].year100_401k = 1133000;
list[2].month100_paycheck = 0;
list[2].taxRate = 0.39;
strcpy(list[3].name, "Andropov, Picov");
strcpy(list[3].title, "Shipping Coordinator");
list[3].empID = 7;
list[3].empStatus = 1;
list[3].year100_salary = 4450000;
list[3].year100_401k = 375000;
list[3].month100_paycheck = 0;
list[3].taxRate = 0.31;
}
for (i = 4; i < siz; ++i) {
strcpy(list[i].name, "none");
strcpy(list[i].title, "none");
list[i].empID = -1;
list[i].empStatus = -1;
list[i].year100_salary = 0;
list[i].year100_401k = 0;
list[i].month100_paycheck = 0;
list[i].taxRate = 0.0;
}
return;
}
void prntWorker(workerT list[], int siz, int indx) {
int i = 0;
for (i = 0; i < siz; ++i) {
printf("%s, ", list[i].name);
printf("%s, ", list[i].title);
printf("%d, ", list[i].empID);
printf("%d, ", list[i].empStatus);
printf("%d, ", list[i].year100_salary);
printf("%d, ", list[i].year100_401k);
printf("%lf, ", list[i].taxRate);
printf("%d ", list[i].month100_paycheck);
printf("\n\n");
}
return;
}
void payWorker(workerT list[], int siz, int indx) {
int i;
for (i = 0; i < siz; ++i) {
list[i].month100_paycheck = (list[i].year100_salary / 12);
}
prntWorker(list, MAX_ROSTER,0);
return;
}
//FIXME FIXME FIXME
int dismissWorker (workerT list[], int siz, int badID) {
int i;
printf("HI");
for (i = 0; i < siz; ++i) { //FIXME
if (list[i].empID == badID) {
printf("HIHIHI");
list[i].empStatus = -1;
list[i].month100_paycheck = 0;
list[i].year100_salary = 0;
return 1;
}
else {
printf("\nWARNING! empID not found! Cannot dismiss "
"a non-employee!\n\n");
return 1;
}
}
return siz;
}
void initCo(companyT hubCo[], workerT roll [], int sizRoll) {
initWorkerArray(roll, sizRoll);
strcpy(hubCo[0].dbaName, "Dewey, Cheatham, and Howe, Consultants");
hubCo[0].EmpActiveTot = 3;
hubCo[0].EmpOnLeaveTot = 0;
hubCo[0].year100_salaryTot = 0;
hubCo[0].year100_401kTot = 0;
hubCo[0].year100_taxTot = 0.0;
hubCo[0].month100_paycheckTot = 0;
}
void doCoBooks(companyT hubCo[], workerT roll[], int sizRoll) {
int i = 0;
for (i = 0; i < sizRoll; ++i) {
if (roll[i].empStatus == 1) {
payWorker(roll, MAX_ROSTER, i);
hubCo[0].year100_salaryTot += roll[i].year100_salary;
hubCo[0].year100_401kTot += roll[i].year100_401k;
hubCo[0].year100_taxTot += roll[i].taxRate;
hubCo[0].month100_paycheckTot += roll[i].month100_paycheck;
}
}
}
void printCoBooks(companyT hubCo[]) {
printf("Name: %s\n", hubCo[0].dbaName);
printf("Active: %d\n", hubCo[0].EmpActiveTot);
printf("Leave: %d\n", hubCo[0].EmpOnLeaveTot);
printf("Salary: %d\n", hubCo[0].year100_salaryTot);
printf("401k: %d\n", hubCo[0].year100_401kTot);
printf("Monthly: %d\n", hubCo[0].month100_paycheckTot);
printf("Tax: %lf\n", hubCo[0].year100_taxTot);
printf("\n\n");
}
In your function initWorkerArray(workerT list[], int siz) you initiate four workerT objects with empID values 1, 2, 6, 7.
Now, in the main function, you call dismissWorker with dismissWorker(roster, MAX_ROSTER, 10)
In the for loop:
for (i = 0; i < siz; ++i) { //FIXME
if (list[i].empID == badID) {
printf("HIHIHI");
list[i].empStatus = -1;
list[i].month100_paycheck = 0;
list[i].year100_salary = 0;
return 1;
}
}
As far as I can see, the code does run through the for loop, but it can't find the badID and exits. You should print a warning or an error message to catch errors, such as these.
Try dismissWorker(roster, MAX_ROSTER, 7) to test it. It should work, and use error / warning messages to catch unforeseen circumstances in the code, such as empID's that don't exist in this case. Commonly known as Defensive Programming
You'll be able to see the error in your logic clearly if you use proper indentation in your code.
int dismissWorker (workerT list[], int siz, int badID) {
int i;
printf("HI");
for (i = 0; i < siz; ++i) {
if (list[i].empID == badID) {
printf("HIHIHI");
list[i].empStatus = -1;
list[i].month100_paycheck = 0;
list[i].year100_salary = 0;
return 1;
}
else {
printf("\nWARNING! empID not found! Cannot dismiss "
"a non-employee!\n\n");
return 1;
}
}
return siz;
}
As you can see, if the ID of the first employee doesn't match the given badID, the function will return with the warning. You need to use:
// Return 1 for success and 0 for failure, maybe.
int dismissWorker (workerT list[], int siz, int badID) {
int i;
printf("HI");
for (i = 0; i < siz; ++i) {
if (list[i].empID == badID) {
printf("HIHIHI");
list[i].empStatus = -1;
list[i].month100_paycheck = 0;
list[i].year100_salary = 0;
return 1;
}
}
printf("\nWARNING! empID not found! Cannot dismiss "
"a non-employee!\n\n");
return 0;
}
In my code the following function exists:
int Count_border(int loc[], int search[], int search_c){
int count = 0, i, j;
for(j = -1; j < 2; j += 2){
if(In_array(BOARD[loc[0] + j][loc[1]], search, search_c) == 1) count++;
}
for(j = -1; j < 2; j += 2){
if(In_array(BOARD[loc[0]][loc[1] + j], search, search_c) == 1) count++;
}
return count;
}
In this function I am searching for values in the array search. How it is done doesn't matter for this question. My question is however, how can I input a "manual" array, like this: Count_border(con_input, {-1, 0, 1}, 3);
This syntaxis isn't allowed by the compiler. And I don't want to create an array before the function, I really want to hardcode it.
Thank you for your help!
EDIT:
Now I am getting this error:
In function 'main':
file.c:40:1: error: expected ';' before '}' token
}
^
file.c:85:1: error: expected declaration or statement at end of input
}
Where this is my whole code, PLEASE help me out.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void Put(int letter, int number);
void Set(int letter, int number, int who);
void Init_board();
int Count_border(int loc[], int search[], int search_c);
int In_array(int val, int arr[], int size);
int BOARD[9][9]; // 0 = empty, 1 = me, 2 = enemy
int STONES[2][81][9][9], STONE_COUNTER[2];
int main(void){
char input[5];
int con_input[2], t;
Init_board();
memset(STONES, 0, sizeof(STONES));
memset(STONE_COUNTER, 0, sizeof(STONE_COUNTER));
scanf("%s", input);
if(strcmp(input,"Start") == 0){
Put(4, 4);
}
scanf("%s", input); //get the first input after start
do{
con_input[0] = input[0]-'a'; /* Convert the input */
con_input[1] = input[1];
Set(con_input[0], con_input[1], 2);
t = Count_border(con_input, (int[]){-1, 0, 1}, 3);
printf("%i\n", t);
scanf("%s", input); /* Get the next input */
} while(strcmp(input, "Quit") != 0)
}
void Init_board(){
int i,j;
memset(BOARD, -1, sizeof(BOARD));
for(i = 0; i < 9; i++){
for(j = 0; j < 9; j++){
BOARD[i][j] = 0;
}
}
}
void Put(int letter, int number){
char t = letter + 'a';
printf("%c%i\n", t, number);
//fflush(stdout);
Set(letter, number, 1);
}
void Set(int letter, int number, int who){
BOARD[letter][number] = who;
}
int Count_border(int loc[], int search[], int search_c){
int count = 0, i, j;
for(j = -1; j < 2; j += 2){
if(In_array(BOARD[loc[0] + j][loc[1]], search, search_c) == 1) count++;
}
for(j = -1; j < 2; j += 2){
if(In_array(BOARD[loc[0]][loc[1] + j], search, search_c) == 1) count++;
}
return count;
}
int In_array(int val, int arr[], int size){
int i;
for (i=0; i < size; i++) {
if (arr[i] == val)
return 1;
}
return 0;
}
/* notes:
fflush(stdout);
*/
If you have a C99 (or newer) compiler just do
Count_border(con_input, (int[]){-1, 0, 1}, 3);
this (int[]){ something } is called a compound literal in C jargon and defines a temporary object (here an int array with 3 elements) that you can pass to your function.
Something like this?
#include <stdio.h>
void f(char arr[]);
int main(int argc, char *argv[])
{
f((char [4]){'1', '2', '3', '5'});
return 0;
}
void f(char arr[4])
{
int i;
for (i = 0; i < sizeof(arr)/sizeof(*arr); i++)
printf("%c ", arr[i]);
putchar('\n');
}