I have to write a function that reads from the keyboard a structure type variable and a function that displays a structure type variable. Subsequently, I have to use these functions to read and display a n number of elements of the structure. That's what I managed to write, but it does not look very correct and logical. I'd be very happy to help. Here's my code :
#include <stdio.h>
struct data{
int d, m, y;
}dt;
void readData(struct data element){
printf("\nData format dd-mm-yyyy : ");
scanf("%d %d %d", &element.d,&element.m,&element.y);
}
void read(struct data element,int n){
for(int i = 0; i < n; i++){
readData(element);
}
}
void display(struct data element){
printf("\n %d.%d.%d\n",element.d,element.m,element.y);
}
void displayN(struct data element, int n){
for(int i = 0; i < n; i++){
display(element);
}
}
int main() {
struct data dd1;
read(dd1,3);
displayN(dd1,3);
return 0;
}
It looks like you want to use an array.
#include <stdio.h>
struct data{
int d, m, y;
}dt;
/* use pointer to an element to modify the data */
void readData(struct data *element){
printf("\nData format dd-mm-yyyy : ");
scanf("%d %d %d", &element->d,&element->m,&element->y);
}
/* use an array to read data (just syntax sugar, this argument element is actually a pointer) */
void read(struct data element[],int n){
for(int i = 0; i < n; i++){
readData(&element[i]);
}
}
/* you don't need to use a pointer here because the value is just printed and not changed */
void display(struct data element){
printf("\n %d.%d.%d\n",element.d,element.m,element.y);
}
/* use an array to print multiple data */
void displayN(struct data element[], int n){
for(int i = 0; i < n; i++){
display(element[i]);
}
}
/* define the number of elements and use that to avoid typo */
#define N 3
int main(void) {
struct data dd1[N]; /* declare an array */
read(dd1,N);
displayN(dd1,N);
return 0;
}
Related
I have two functions. One that creates a multiplication table of a given number and the other function prints the array out. Below is my code:
Here's the error (Line 18):
expression must be a pointer to a complete object type
How do I fix this error and print the array? Also, I don't know how to print a new line after every row.
#include "multiplication.h"
#include <stdio.h>
int arr[][];
void mulitpication(int num){
/* initialize array and build*/
int arr[num][num];
for(int i=0; i<num;i++){
for(int j=0;j<num;j++){
arr[i][j]= (i+1)*(j+1);
}
}
}
void print(int arr[][]){
/* print the arr*/
int i;
for(i=0;i<sizeof(arr);i++){
for(int j=0;j<sizeof(arr);j++){
printf("%d ",arr[i][j])**(line 18)**;
}
}
}
If using C99 or later with VLA support, pass into the print function the dimensions needed.
// void print(int arr[][]){
void print(size_t rows, size_t cols, int arr[row][col]){
size_t r,c;
for (size_t r = 0; r < rows; r++) {
for (size_t c = 0; c < cols; c++) {
printf("%d ",arr[r][c]);
}
printf("\n");
}
}
You need to declare the array in main(), so it can be passed to both functions.
When an array is passed as a function parameter, it just passes a pointer. You need to pass the array dimensions, they can't be determined using sizeof.
To get each row of the table on a new line, put printf("\n"); after the loop that prints a row.
#include <stdio.h>
void multiplication(int num, arr[num][num]){
/* initialize array and build*/
for(int i=0; i<num;i++){
for(int j=0;j<num;j++){
arr[i][j]= (i+1)*(j+1);
}
}
}
void print(int num, int arr[num][num]){
/* print the arr*/
int i;
for(i=0;i<num;i++){
for(int j=0;j<num;j++){
printf("%d ",arr[i][j]);
}
printf("\n");
}
}
int main(void) {
int size;
printf("How big is the multiplication table? ");
scanf("%d", &size);
int arr[size][size];
multiplication(size, arr);
print(size, arr);
return 0;
}
#include<stdio.h>
struct Ques
{
int a;
}Q[5];
void sort(int a[])
{
printf("any sort technique...");
}
void main()
{
sort(Q.a);
}
So this is the sample code.
I Want to access the whole struct element as array.
You want this:
#include<stdio.h>
struct Ques
{
int a;
} Q[5];
void sort(struct Ques array[], int size)
{
printf("any sort technique...");
// just some demo
for (int i = 0; i < size; i++)
printf("array[%d].a = %d\n", i, array[i].a);
}
int main()
{
// put some data into Q
sort(Q, 5);
}
sort needs two parameters:
the pointer to the array to sort
the size of the array (unless you only ever want to sort array of some fixed size
Hi this code is just an example of what im working on. I've got the solution by copying everything from my search function and dump it into my edit function. Is there any better solution rather than just copy pasting?
struct inventory
{
float a,b,c,d;
char something[MAXSIZE];
};
typedef struct inventory Inventory;
struct will contain some float integer and some characters.
void search(const Inventory inv[], int np); // declare search function
void edit(struct inventory inventoryRegister[],int np);
int main(void)
{
int np=0;
struct inventory inventoryRegister[MAXSIZE];
// calling the functions search and edit
search(inventoryRegister, np);
edit(inventoryRegister, np);
return 0;
}
void search(const Inventory inv[], int np)
{
int i,
float min, max;
printf("Enter min max");
scanf("%f %f", &min, &max);
for (i = 0; i < np; i++)
if (inv[i].a >= low && inv[i].a <= high)
{
print..
}
//repeat for b,c,d and something
}
void edit(struct inventory inventoryRegister[],int np)
{
int a;
print("Enter new a");
scanf("%f", &a);
// Here i can copy and paste my entire search function and do a loop to replace the min & max with my new input a.
But is there any easier way to do it? say i call the search(); and somehow extract the elements between min & max and do a loop replacement with a?
Any suggestion?
}
Disclaimer : Its a kind of a psuedo code
struct MinMaxLocation
{
int minloc;
int maxloc;
};
struct MinMaxLocation getMinMaxLocation(struct inventory inventoryRegister[],float min,float max)
{
// your logic to find min and max locations here
struct MinMaxLocation loc;
loc.minloc = // min element location
loc.maxloc = // max element location
return loc;
}
void search(....)
{
struct MinMaxLocation loc_here_search = getMinMaxLocation(inventoryRegister,min,max);
for (i = loc_search_here.min; i < loc_search_here.max; i++)
{
print ...
}
}
void edit(....)
{
struct MinMaxLocation loc_here_search = getMinMaxLocation(inventoryRegister,min,max);
for (i = loc_search_here.min; i < loc_search_here.max; i++)
{
edit the values here ...
}
}
For reading some data that describes the coordinates of lines I wrote the following code:
int numLines;
scanf("%d", &numLines);
int xStart, yStart, xEnd, yEnd;
for (int i = 0; i < numLines; i++) {
scanf("%d %d %d %d", &xStart, &yStart, &xEnd, &yEnd);
}
But I think it would be more useful for the rest of the program if I stored the data into multidimensional array. How do I do that and which is better: storing the data into one 4-dimensional or two 2-dimensional arrays?
Your first scanf() is wrong, you need to tell scanf() what to scan for, that is doen by means of the specifiers.
Without specifiers, it will interpret the passed parameter as the format string, which will cause problems, so you need
if (scanf("%s", &numLines) == 1)
{
for (int i = 0 ; i < numLines ; ++i)
{
if (scanf("%d %d %d %d", &xStart, &yStart, &xEnd, &yEnd) == 4)
{
/* process the data here */
}
}
}
You don't really need an array of so many dimentions, you can use a struct for that, something like
struct Data
{
int xStart;
int xEnd;
int yStart;
int yEnd;
};
now, you can create an array of structs and many other things, and when using it you just need
struct Data data[SIZE];
int j;
j = 0;
if (scanf("%s", &numLines) == 1)
{
for (int i = 0 ; ((i < numLines) && (j < SIZE)) ; ++i)
{
if (scanf("%d %d %d %d",
&data[j].xStart,
&data[j].yStart,
&data[j].xEnd,
&data[j].yEnd) == 4)
{
j++;
}
}
}
I would even go further, and define
struct Item
{
int start;
int end;
};
struct Items
{
struct Item x;
struct Item y;
};
which would make the code more readable and understandable.
I would suggest to use structure.
For example
struct Line{
int startX;
int startY;
int endX;
int endY;
};
And then use array of this structure.
Rather than multidemensional array, I recommend to use 2 structs. This is more natural.
#include <stdio.h>
#define numlines 10
struct point {
int x;
int y;
};
struct line {
struct point a; /* start */
struct point b; /* end */
};
struct line lines[numlines];
int main() {
int i;
for ( i = 0; i < numlines; i++ ) {
scanf("%d %d", &lines[i].a.x, &lines[i].a.y);
scanf("%d %d", &lines[i].b.x, &lines[i].b.y);
}
}
I have the following code of declarations:
struct coord {
int x;
int y;
}
void rotateFig(struct coord[10][10]);
I need to implement rotateFig.
I tried to start with following:
void rotateFig(struct coord[10][10] c)
{
//code
}
I can`t compile it - probaly the way I transfer c in the function definition is incorrect .How should I transfer c using given signature.
Thank you
Use this definition:
void rotateFig(struct coord c[10][10])
{
//code
}
The array is the actual parameter, so the dimensions have to come after its name, not before.
Though other answers ARE enough, I prefer passing it as a pointer and passing the dimensions with it, this is more dynamic, and is the same for the //code part:
void rotateFig(struct coord**, int, int);
void rotateFig(struct coord** c, int d1, int d2)
{
//code
}
struct coord is a type and c is a variable of type struct coord which can hold 10 X 10 struct coord elements.
So it should be as follows
void rotateFig(struct coord c[10][10])
One thing to note when working with multi-dimension array in C is that it cannot be return back from a function. For details, read this. So its not advised to use the above format as C by default passes arguments by value and not by address.
So as #Mr.TAMER mentioned in his answer, you should use the following
void rotateFig(struct coord** c, int d1, int d2)
OTOH, you can use the following rotate code for your reference! It rotates a 2d array to 90 degrees!
#include <stdio.h>
#define N 10
int matrix[N][N];
void display()
{
int i, j;
printf("\n");
for (i=0; i<N; i++) {
for (j=0; j<N; j++)
printf("%3d", matrix[i][j]);
printf("\n");
}
printf("\n");
return;
}
int main()
{
int i, j, val = 1;
int layer, first, last, offset, top;
for (i=0; i<N; i++)
for (j=0; j<N; j++)
matrix[i][j] = val++;
display();
for (layer = 0; layer < N/2 ; layer++) {
first = layer;
last = N - layer - 1;
for (i=first; i< last ; i++) {
offset = i - first;
top = matrix[first][i];
matrix[first][i] = matrix[last-offset][first];
matrix[last-offset][first] = matrix[last][last-offset];
matrix[last][last-offset] = matrix[i][last];
matrix[i][last] = top;
}
}
display();
return 0;
}
Hope this helps!