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);
}
Related
When I simply run it in Clion, it works all right. While I am trying to debug it, it only printf after all the scanf is completed.
Below is a simple program to verify whether the array containing three values can add up to a target with either two of the three values.
This is the effect after debugging.
#include "stdio.h"
#include "stdlib.h"
int twoNums(int *nums, int numsSize, int target, int *returnSize);
int main() {
system("chcp 65001");
int string[3], target;
int numsSize = 3;
int isTrue;
int *returnSize = malloc(sizeof(int) * 2);
printf("Please input the array to be queried:\n");
scanf("[%d,%d,%d]", &string[0], &string[1], &string[2]);
printf("Please input the target value:\n");
scanf("%d", &target);
isTrue = twoNums(string, numsSize, target, returnSize);
if (isTrue)
printf("indexs of these is (%d, %d)", returnSize[0], returnSize[1]);
else
printf("something went wrong!");
free(returnSize);
}
int twoNums(int *nums, int numsSize, int target, int *returnSize) {
int i, j, cnt;
int flag = 0;
for (i = 0; i < numsSize; i++) {
cnt = target - nums[i];
for (j = i + 1; j < numsSize; j++) {
if (nums[j] == cnt) {
flag = 1;
break;
}
}
if (flag == 1)
break;
}
if (flag == 1) {
returnSize[0] = i;
returnSize[1] = j;
return 1;
} else {
return 0;
}
}
Why would this happen? How can I solve this problem?
I have been solving a problem of CyclicRotation. This is my code:
#include<stdio.h>
#include<stdlib.h>
struct Results{
int *A;
int N;
};
struct Results solution(int A[], int N, int K){
struct Results result;
int *tab, i=0, j=0;
tab = (int*) malloc(N*sizeof(int));
if(N==0){
result.A = A;
result.N = N;
return result;
}
if(K>N){
K = K % N;
}
if(K<N && N != 0){
for(i=N-K;i<N;i++){
tab[j] = A[i];
j = j + 1;
}
i = 0;
while(i<N-K){
tab[j] = A[i];
i++;
j++;
}
} else {
tab = A;
}
result.A = tab;
result.N = N;
return result;
}
int main()
{
int j[]={2,3,4,5,6,7,8};
int mylen;
int myk = 3;
mylen = sizeof(j)/sizeof(j[0]);
return 0;
}
I tried this in the code:
int main()
{
int j[]={2,3,4,5,6,7,8};
int mylen;
int myk = 3;
mylen = sizeof(j)/sizeof(j[0]);
printf("The result is %d",solution(j,mylen,myk).A);
return 0;
}
The expected result is 6782345, but the result in the console is different:
The result is -591373584
I'm not sure if the array printing is correct (given that is an array, perhaps I need a loop?). Please, could you give me a help? Thank you in advance.
int main()
{
int j[]={2,3,4,5,6,7,8};
int mylen;
int myk = 3;
mylen = sizeof(j)/sizeof(j[0]);
struct Results results = solution(j, mylen, myk);
for(int i = 0; i < mylen; i++)
printf("%d\n", results.A[i]);
free(results.A);
return 0;
}
There was a breakfast organised by social workers. There were many people in line to get the food packet. Find out first person who reappeared in line for food packet quickly.
Given
Adam, Binny, Sohail, Krishna , Mayank, Sohail, Adam
The output should be:
Sohail
I am unable to solve this in c language.
Please can somebody explain what to do?
This is my code:
#include<stdio.h>
int main()
{
int n,i,j;
char str[20][20];
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s",&str[i]);
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(str[i]==str[j]){
printf("%s\n",str[i]);
return 0;
}
}
}
return 0;
}
I am getting nothing in output.
It can give you some directions:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct PersonInfo
{
char *name;
int number;
} person_info;
void check(person_info arr[], size_t size);
int main()
{
char *arr_debug[20] = {"Adam", "Binny", "Sohail", "Krishna", "Mayank", "Sohail", "Adam"};
int i = 0;
person_info arr[7];
size_t size = sizeof(arr)/sizeof(arr[0]);
person_info temp;
for (i = 0; i < size; i++) {
// scanf("%s", temp.name);
temp.name = arr_debug[i];
temp.number = i + 1; // start counting from 1
arr[i] = temp;
}
check(arr, size);
return 0;
}
void check(person_info arr[], size_t size) {
char *temp_name;
int nums[size]; // filled with 0s
int num = 0;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (strcmp(arr[i].name, arr[j].name) == 0 && arr[i].number < arr[j].number) {
nums[num] = arr[j].number;
num++;
//store the number of the person
}
}
}
//find the earliest occurence
int earliest = nums[0];
for (int i = 1; i < size; i++) {
if (earliest > nums[i] && nums[i] != 0) {
earliest = nums[i];
}
}
//find the name associated to the number
for (int i = 0; i < size; i++) {
if (arr[i].number == earliest) {
temp_name = arr[i].name;
printf("%s \n", temp_name);
}
}
}
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;
}