answersLogoWhite

0


Best Answer

Eg:

int main (int argc, char **argv)

{

int i;

printf ("array argv has %d elements:\n", argc);

for (i=0; i

return 0;

}

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

Arrays are not ideally suited to queues. Firstly, to avoid resizing the array every time you add a new element, you have to reserve space in the array, and only resize when there are too few unused elements, or too many. Secondly, a queue is a first in first out structure (or last in last out), so while insertions are easily dealt with by placing new data in the first unused element, extractions will result in unused elements at the beginning of the array, which means you must shunt everything forward whenever there aren't enough free elements at the end of the array.

These are not major problems, but they do get in the way of the function of a queue. The purpose of a queue is to push elements to the back and pop from the front, both of which must ideally be done in constant time. Reserving space and shunting elements affects this process immensely, even if it only happens occasionally. However, as well as storing the size of the array, you also need to store the index of the first used element (the head of the queue), and the last used element (the tail of the queue), and adjust these as elements are inserted and extracted. The number of used elements is last-first+1, and the number of unused elements is size-used. During an insertion, if size-last is 1, but size-used is not zero, you must shunt every element forward by that amount. If size-used is zero, you must resize the array. Then you can insert and update the indices. For an extraction, if last is less than first, then there are no elements to extract, otherwise you extract first and then increment first. You also have the option at that point to shunt everything else forward by first elements, if first is greater than a predetermined threshold, or postpone until an insertion actually requires the unused space.

Of course, the best structure for a queue is a singly-linked, circular list. The list simply maintains a pointer to the tail, which itself points to the head, thus permitting constant time access to both. When you insert a new node, the tail node's next node (which is the head) is assigned to its next node, while the new node is assigned to the tail node's next node. The new node is then assigned to the list's tail node. To extract an element, you extract the head node's data and assign its next node to the tail node's next node, before deleting the head node (the tail node's next node becomes the new head). If the tail node points to itself, then there is only one element in the list (the tail is also the head). If the tail is NULL, the list is empty.

It sounds more complex than it really is, but the upshot is that the list is self-maintaining, doesn't require data to be moved around in memory, requires no redundant nodes and guarantees constant time access for every insertion or extraction. Some additional memory is required to maintain the pointers (1 pointer per node, plus 1 for the tail node pointer), but unlike an array, every bit is doing something useful. Some additional time is also required to create and destroy individual nodes, but these are constant time operations. With an array, even if you postpone resizing until a threshold is reached, you cannot guarantee constant time insertion, you can only guarantee constant time extraction (assuming you don't use a forward shunt threshold).

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

#include<iostream>

#include<time.h>

// Some functions we can point at (same signature)

int square(int x){return(x*x);}

int cube(int x){return(x*x*x);}

// Define a function pointer type.

typedef int (*pfunc) (int);

// Initialises an array with random integers.

void randomise(int* a, size_t elements)

{

for(size_t index=0; index<elements; ++index)

a[index] = rand() % 100;

}

int main()

{

// Seed the random number generator.

srand((unsigned)time(NULL));

// Instantiate a function pointer.

pfunc f=NULL;

// Instantiate a static array of 10 elements.

const size_t elements=10;

int a[elements];

// Initialise the array.

randomise(a, elements);

// Point to the first element in the array.

int *p=&a[0];

// Determine the address of last element in the array.

int *last=p+elements-1;

// Repeat while p points to an element within the array.

while(p<=last)

{

// Dereference the pointer and store the value.

int b=*p;

// If the value is odd, point to the square function,

// otherwise point to the cube function.

if(b&1)

f=square;

else

f=cube;

// Call the function and store the result.

int c = f(b);

// Print the result.

std::cout<<b<<" => "<<c<<std::endl;

// Increment the pointer by sizeof(int) bytes.

++p;

}

}

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

The following is an example of a minimal C program with just one function that does absolutely nothing whatsoever:

#include<stdio.h>

main()

{

}

A more useful example that calls only one built-in function is the standard 'hello world' example.

#include<stdio.h>

main()

{

printf("Hello world!");

}

It is clearly possible to write an entire program using just one function, but in order to be useful, you have to call at least one built-in function. But other than trivial programs such as those shown above, you should always break your programs down into smaller, easy to manage functions, that can be called as and when required, thus eliminating the need to continually write duplicate code, which only serves to increase maintenance.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Queues are collections of data where elements are added to the last position and removed from the first position. Queues are also referred to as FIFO (First In First Out) data structures - the first item added to the queue is the first item removed. A queue can be implemented using an array or a linked list.

Using an array, a queue can be coded using one of two systems: a circular buffer and, for lack of a more official term, a "shifting" buffer.

1) Circular Buffers

Circular buffers maintain two integer offsets to a buffer: the head and the tail. The head and tail both point to '0' when the buffer is initialized. When an item is added to the buffer, it's stored in the tail position, and the tail position increments. When an item is removed from the buffer, it's copied from the head position, and the head position increments.

If either the head or tail moves past the last offset in the array, it's reset to zero (the first position). Also, if the array is static in nature (it neither increases nor decreases in size), then when the tail position reaches the head position it overwrites the data at the head position, while the head increments.

A sample structure for maintaining a circular buffer of integers follows:

struct circbuffer {

int *buffer;

int head, tail, maxitems;

} CircBuffer;

buffer is first initialized to malloc(sizeof(int)*maxitems), where maxitems is set to the maximum number of items in the buffer. head and tail are initialized to 0.

When an item is added, it's stored at buffer[tail], and tail is then incremented. If tail is equal to maxitems, it's reset to 0.

When an item is removed, it's copied from buffer[head], and head is then incremented. If head is equal to maxitems, it's reset to 0.

If head is equal to tail, there are no items in buffer.

2) Shifting Buffers

A shifting buffer, on the other hand, only maintains a "last item index" to the last item in the array. It's set to zero when the buffer is initialized. When an item is added, it's stored at the last item index, which is then incremented. When an item is removed from the buffer, all of the contents after the first item are shifted to overwrite that item, and the last item index is decremented.

Shifting buffers are slower, because memory contents have to be copied (using memmove()) any time items are removed. Thus, it's recommended programmers use a circular buffer to implement a queue.

If the queue must resize, consider using a linked list, because it is far less computationally expensive.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

#include <stdio.h>

static void Hello (void)

{ puts ("Hello, World"); }

int main (void)

{ Hello (); return 0; }

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a c program using functions and array?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you write a program to read set of numbers using by an array and display the ascending order of the given input numbers?

To write a C++ program to display the student details using class and array of object.


How do you write a program in C using the fprint and fscan functions?

By using those two functions in your code.


Write c program to find median?

If you are using an array : sort using qsort() then take middle element.


How do you write an assembly language program to find the sum of n numbers using array?

write an assembly language program to find sum of N numbers


How do you write c program to perform sum of elements of matrix using pointers with functions?

i cant write


You want to write a simple without using pointer or array c program which will print greatest number when you give 20 number?

i want to write a simple without using pointer or array c program which will print greatest number when i give 20 number .........How far have you gotten so far?


Would you Write c plus plus program using array for Fibonacci number?

You can write a C++ fib pro using arrays but the problem is the prog becomes very complicated since u need to pass the next adding value in an array.....


Write C program using functions to simulate a Calculator with the basic functions - square root and reciprocal?

trytytytytytytytytrf 6 bcvvtgujhy6


Write a c program to reverse an array without using another array?

public static int[] reverseArray(int[] array) { int i = 0, j = array.length - 1; for (i = 0; i &lt; array.length / 2; i++, j--) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } return array; }


Why we write java program using classes?

Classes are well organised functions in java which help discriminate between two different functions.


Write a c user defined functions to put n real number in a single dimenssion array compute their meanvariancestandarad deviation using these functions write a c program?

#include&lt;stdio.h&gt; #include&lt;conio.h&gt; int arr(int a[]); void main() { int a[10],i; clrscr(); printf("Enter the value for array") for(i=0;i&lt;10;i++) { scanf("%d",&amp;a[i]); } arr(a); getch(); } int arr(int a[10]) { int i; printf("Elements of array\n"); for(i=0;i&lt;10;i++) { printf("\n%d",a[i]); } return 0; }


How do you write a C Program to fill up an Integer Array?

Reference:cprogramming-bd.com/c_page1.aspx# array programming