Getting issues for redefinition of functions - c

I'm trying to scan numbers into 2 2D arrays, and I keep on getting the error of redefinition.
The code:
#include <stdio.h>
#define N 3
void getMatrix(double mat[N][N]);
/*
char getMenuOption();
void getCoordinates(int*, int*);
void sumMatrices(double mat1[][N], double mat2[][N]);
void changeMatrix(double mat[][N]);
void printMatrix(double mat[][N]);
*/
int main() {
double A[N][N], B[N][N];
/*
char option;*/
getMatrix( A[N][N]);
getMatrix( B[N][N]);
/*
option = getMenuOption();*/
return 0;
}
void getMatrix(double A[N][N]){
int i;
for(i=0;i<=N;i++){
for(i=0;i<N;i++)
{
scanf("%lf",&A[N][N]);
}
}
return;
}
void getMatrix(double B[N][N]){
int i;
for(i=0;i<=N;i++){
for(i=0;i<N;i++)
{
scanf("%lf",&B[N][N]);
}
}
return;
}
I guess the problem is that the same function is called twice, but im not so sure about it.
If anyone can help me point to the problem, it will be most welcome.

You don't need to define a function twice (to call it twice or more). One function can be called multiple times, that's the reason of having functions in first place. Get rid of
void getMatrix(double B[N][N]){
int i;
for(i=0;i<=N;i++){
for(i=0;i<N;i++)
{
scanf("%lf",&B[N][N]);
}
}
return;
}
Having said that, you should call the function like
getMatrix(A);
getMatrix(B);
To pass the array (the decay to pointer, anyway). The notation A[N][N] denotes a member of the array and for an array defined like
double A[N][N];
it's off-by-one, as array indexing in C starts from 0.

The function is defined twice
First definition
void getMatrix(double A[N][N]){
int i;
for(i=0;i<=N;i++){
for(i=0;i<N;i++)
{
scanf("%lf",&A[N][N]);
}
}
return;
}
Second definition
void getMatrix(double B[N][N]){
int i;
for(i=0;i<=N;i++){
for(i=0;i<N;i++)
{
scanf("%lf",&B[N][N]);
}
}
return;
}
Take into account that these calls of the function are invalid
getMatrix( A[N][N]);
getMatrix( B[N][N]);
The arguments have type double instead of arrays or pointers.
You should remove one definition of the function and declare the function correctly.
If the compiler allows to use Variable Length Arrays then the functiuon should be declared like
void getMatrix(size_t n, double A[n][n]);
if Variable Length Arrays are not supported by the compiler then N must be a constant and the function indeed can be declared like
#define N SOME_VALUE
//...
void getMatrix( double A[N][N] );
and call the function like
in the first case
getMatrix( N, A );
getMatrix( N, B );
and in the second case
getMatrix( A );
getMatrix( B );

Related

Sorting a 30 element array in C

Im trying to do a program that sorts 30 elements of an array.Im getting an sintaxe error, i was hoping someone could help me. I think it's happening when im calling the SelectionSort function, havent written c in a while, so i guess its sintaxe. valores means values, and valoresordenados means sorted values.
#include <stdio.h>
#include <stdlib.h>
#define N_max 30
void SelectionSort (int *valores[], int n)
int main(void){
int n,j,valores[],N_max;
n=N_max; // Defining n as the number of elements in the arrray
for(j=0;j<n;j++){
valores[j]=30-j; //Inserting values in the array's positions
}
SelectionSort(int valores[n], int n); //Calling the subprogram
printf("valoresordenados[%d]= %d",j, valores[j]);
system("pause");
return (0);
}
void SelectionSort (int *valores[], int *n) { //Subprogram that sorts the values of the array
int j,z,min,aux;
for(j=0;j<n-1;j++){
min=j;
for(z=j+1;z<n;z++){
if(valores[z]<valores[min])
min=z;
}
}
if(valores[j]!=valores[min]){
aux=valores[j];
valores[j]=valores[min];
valores[min]=aux;
}
}
Notice that your prototype looks different to the actual function.
void SelectionSort (int *valores[], int n); // u forgot ;
vs.
void SelectionSort (int *valores[], int *n)
which takes an address to an integer instead of an integer.
Also this
SelectionSort (int valores[n], int n); // Calling the subprogram
is not valid syntax, you need to write (and remove the * in front of n from the function prototype)
SelectionSort (valores, n); // Calling the subprogram
You're missing a semi-colon ';' at line 5
void SelectionSort (int *valores[], int n);
There may be the problem with your function call.
It should be like :
SelectionSort(valores, &n);
This is the syntax error, I think you are getting.

dought in returning array and int data type from function in c

code 1
main()
{
int i ,a[5];
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
yo(a);
for(i=0;i<5;i++)
{
printf("%d ",a[i]);
}
}
void yo(int a[5])
{
int i;
for(i=0;i<5;i++)
{
a[i]=a[i]+1;
}
}
in the above code with out returning values(i am returning void data type in function) the array 'a' is getting updated in main function but when i don't use array and use normal int data type the values does'nt get updated see code 2
main()
{
int a;
a=50;
yo(a);
printf("%d",a);
}
void yo(int z)
{
z=150;
}
It is because the array a[] is being passed as a pointer, not a copy of the array, and the array is modified directly. If you declare the function as
void yo(int *a)
that will work in exactly the same way. But if you pass a single int such as
void yo(int a) {
a += 1;
}
the function only receives a copy of the int and it does not affect the caller. To affect the variable passed, you would have
void yo(int *a) {
*a += 1;
}
And guess what? That's the same declaration as I put earlier when you pass an array. When you pass a pointer, it can be treated as an array, or as a single value - namely an array of length 1.

Function which receives a 1 d array and print its value there?

I wanted to write a function which can receive a 1-D array and print its value there.Also wanted to know how 2-D array can be received by a function and print its value there.
In C you pass arrays by pointers, and usually a second parameter, which contains its length.
For Example: void printArray(char * arrayStart, int length) (for a char array)
and I assume you know how to write a simple for-loop to iterate over all elements of your array and print them. For 2D Arrays you would use char ** arrayStartinstead.
(When calling the function you pass the array in the following fashion:
char myArray[] = "some Text"
printArray(myArray, 9);
)
sample code here:
#include<stdio.h>
void print_1D(int *arr,int m)
{
int i;
for(i=0;i<m;i++)
printf("%d ",arr[i]);
putchar('\n');
}
void print_2D(int *arr[num],int m,int n) //<---observe here
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d ",arr[i][j]);
putchar('\n');
}
}
int main()
{
int oneD[anySize1] = {2,5,67,23,32,....};
int twoD[anySize2][num] = { {23,17,..},{....},{....},....}; //<---and here
int m = anySize1,n = anySize2;
print_1D(oneD,m);
print_2D(twoD,m,n);
return 0;
}
if 2d is array is declared like this
int arr2D[10][20];
then function declaration must be,
void print_2D(int *arr[20],int m,int n)
or
void print_2D(int arr[][20],int m,int n)

output error in printing number in c

int funcc(int a[],int b[],int *cnt)
{
int *c;
int j,i,s=0;
for (i=0;i<n;i++)
for (j=0;j<n;j++)
if(b[i]==a[j])
{
*cnt++;
break;
}
c=(int*)malloc(*cnt*sizeof(int));
for (i=0;i<n;i++)
for (j=0;j<n;j++)
if(b[i]==a[j])
{
c[s++]=b[i];
break;
}
return c;
}
void main (void)
{
int *c;
int *cnt=0;
int i,arr[n]={3,2,1},brr[n]={3,2,0};
c=funcc(arr,brr,&cnt);
for(i=0;i<*cnt;i++)
printf("%d ",c[i]);
}
I need to print the common numbers in 2 arrays..
but the problem is with "cnt" .. if i replace cnt with 3 it works ..
but when i put cnt it doesnt work
The problem is you declare cnt as an int pointer, so when you pass in &cnt, you're passing in a pointer to a pointer to an int. Try changing that second line of main to int cnt=0; and change the for loop to for(i=0;i<cnt;i++) (notice the removal of the * character).
EDIT: the line *cnt++; should be changed to either ++*cnt; or (*cnt)++, since the increment operator takes higher precedence than the dereference operator.
Your prototype is:
int funcc(int a[],int b[],int *cnt)
but you are passing it a pointer to a pointer:
int *cnt=0; /* <- pointer */
int i,arr[n]={3,2,1},brr[n]={3,2,0};
c=funcc(arr,brr,&cnt); /* &cnt <- pointer to a pointer */

removing duplicate values from an array in C

I am trying to create a program in C that removes duplicate values in an integer array. My strategy is to first sort the array via a selectionsort function, and then call a function removedup that removes any consecutive, duplicate values in the array.
My code:
#include <stdio.h>
#include "simpio.h"
#define n 10
void GetArray(int a[]);
void SelectionSort(int a[]);
int FindMax(int a[], int high);
void swap(int a[], int p1, int p2);
int removedup(int a[]);
void printArray(int a[]);
main()
{
int a[n];
GetArray(a);
SelectionSort(a);
printf("The original, sorted array is\n");
printArray(a);
printf("The array with removed duplicates \n");
printArray(removedup(a));
getchar();
}
void GetArray(int a[])
{
int i;
for(i=0;i<n;i++)
{
printf("Enter integer# %d", i+1);
a[i]=GetInteger();
}
}
void SelectionSort(int a[])
{
int i, max;
for(i=0;i<n;i++)
{
max=FindMax(a,n-i-1);
swap(a,max,n-i-1);
}
}
int FindMax(int a[], int high)
{
int i, index;
index=high;
for(i=0;i<high;i++)
{
if(a[i]>a[index])
index=i;
}
return index;
}
void swap(int a[], int p1, int p2)
{
int temp;
temp=a[p2];
a[p2]=a[p1];
a[p1]=temp;
}
int removedup(int a[])
{
int i, count, OutArray[count], j;
count=0;
for(i=0;i<n-1;i++)
{
if(a[i]==a[i+1])
{
a[i+1]=a[i+2];
count++;
}
}
count++;
for(j=0;j<count;j++)
{
OutArray[i]=a[i];
}
return OutArray;
}
I have two questions:
1) How do I fix the error the compiler in giving me in the main body when calling removedup inside the printarray function, saying "invalid conversion from int to int*"? (line 22)
2) How do I accurately define the size of OutArray[] in the removedup function? Currently I have it defined as the size variable, but the value of this variable isn't accurately defined until after the declaration of OutArray.
Notice your prototypes ...
int removedup(int a[]);
void printArray(int a[]);
And also notice you're calling printArray() with the result of removedup().
printArray(removedup(a));
The result of removedup() is an int; printarray() requires a int[].
int and int[] are not compatible.
I suggest you remove duplicates and print array in two distinct statements.
You should be able to fix the compiling problems after reading comp.lang-c FAQ on arrays and pointers.
After you get your array sorted, you can use the following function to remove the duplicates:
int dedup(int arr[], int size) {
int curr = 0, next = 0;
while (next < size) {
while (next < size && arr[next] == arr[curr])
next++;
if (next < size)
arr[++curr] = arr[next++];
}
return size ? curr+1 : 0;
}
It takes two arguments, the array and its size. The duplicates are removed in-place, which means that the array is modified, without allocating a new array to store the unique elements.
Remember that the dedup function expects the elements to be sorted! I've noticed you are using your own implementation of selection sort, which makes me think this is homework. In that case, I feel a little reluctant on giving you a complete solution, although understanding it should be a good exercise anyway.
EDIT: I should've explained the last line of code.
return size ? curr+1 : 0; is equivalent to:
if (size)
return curr+1;
else
return 0;
Just a shorter way of saying the same thing.

Resources