I am making a program the enqueues a number at index 0. I also made a function for it, as written below:

//main
#include <stdio.h>
#include <stdlib.h>
#include "functions.h"
main(){
	int *queue = NULL;
	int size, choice, aNumber, numberOfQueues;
	
	do{
		printf("Enter Array size: ");
		size = input();
		allocateMemory(&queue, size);
		numberOfQueues = 0;
		printf("Array of %d queues has been generated.\n", size);	
	}while(size == 0);
	
	do{
		printMenu();
		choice = input();
	
		switch(choice){
			case 1:
				if( numberOfQueues == size ){
					printf("Queue is full. Free by dequeuing or exiting the program.");
				}else{
					printf("Enter a number: ");
					aNumber = input();
					enqueue(queue, aNumber, &numberOfQueues);
					printf("Input has been placed into the queue. Number of queues: %d\n", numberOfQueues);
				}
				
				break;
			
			case 2:
				
				break;
			
			case 3:
				if(numberOfQueues == 0){
					printf("Nothing to print in the queue.\n");
				}else{
					printQueue(queue, numberOfQueues);
				}
				
				break;
				
			case 4:
				printf("Bye! ( ^-^)^");
				free(queue);
				break;
			
			default:
				break;
		}
	}while(choice != 4);
	

}
//functions.h
#include <stdio.h>
#include <stdlib.h>

int input();
void printQueue(int *, int);
void allocateMemory(int **, int);
void enqueue(int *, int, int *);
void dequeue(int *, int);

void printMenu(){
		printf("Menu:\n\n");
		printf("[1] Enqueue\n");
		printf("[2] Dequeue\n");
		printf("[3] Print Queue\n");
		printf("[4] Quit\n");
		printf("Choice: ");
}

int input(){
	int a;
	scanf("%d", &a);
	return a;
}

void printQueue(int *queue, int numberOfQueues){
	int i;
	for(i=0 ; i < numberOfQueues ; i++){
		printf("%10d\t", *(queue+i) );
	}
	putchar('\n');
}

void allocateMemory(int **queue, int size){
	*queue = (int *) malloc ( size * sizeof(int) );
}

void enqueue(int *queue, int newValue, int *numberOfQueues){
	int i;
	
	if(numberOfQueues==0){
		queue[0]=newValue;
	}else{
		for(i=1 ; i < *numberOfQueues ;i++){
			queue[i]= queue[i-1];
		}
		queue[0]=newValue;
	}
	
	*numberOfQueues = *(numberOfQueues)+1;
	
}

void dequeue(int *queue, int size){

}

Problem is, when I print the queue, it prints the new value at index 0, the value of index 1 from there to size-2, and a large value at the end of the array, as tried in a program:

Size 5, queue full. Input values are(from earliest to latest): 3,2,5,7,100:

100 7 7 7 1547322173

You have few problems here. First and foremost, the loop at lines 44-46 does not shift a queue, but rather replicates the leading element. You need to run the loop in other direction, that is shift the last element, then one before last, etc.

Funny enough, the typo at line 41 (I presume you intended *numberOfQueues there) proves that the whole if/else combination is bogus; the control always goes to the else clause. Special-casing 0 is indeed unnecessary.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.