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.
Related
so I'm trying to make a 2d binary matrix that is the size provided by stdin and that has randomly assigned indexes for the 0 and 1, however, their cannot be more than size/2 zeroes or ones.
For example
an input of 2
could output
1 0
0 1
Now I was going to originally just use the argument int arr[][n] in init but this idea failed since passing in the matrix just resulted in my program going on some sort of an infinite loop when I attempted to access matrix again inside of the main function. I believe this happened because the lifespan of matrix expired when init concluded? So my question here is why is what I'm doing now producing the below error and how can I fix this up?
note: expected ‘int * (*)[(sizetype)(n)]’ but argument is of type ‘int (*)[(sizetype)(dim)][(sizetype)(dim)]’
7 | int init(int n, int* arr[][n]);
My code
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int init(int n, int* arr[][n]);
int main(){
time_t t;
srand((unsigned) time(&t));
int dim;
printf("Enter a positive even integer: ");
scanf("%d",&dim);
if(dim<2||dim>80){
return 1;
}
int matrix[dim][dim];
init(dim,&matrix);
return 0;
}
int init(int n, int* arr[][n]){
int numZeroes,numOnes;
int zero_or_one;
for(int row=0;row<n;row++){
numZeroes=0;
numOnes=0;
for(int col=0;col<n;col++){
if(numZeroes<n/2 && numOnes<n/2){
zero_or_one=rand()%2;
*arr[row][col]=zero_or_one;
if(zero_or_one==1){
numOnes++;
}
if(zero_or_one==0){
numZeroes++;
}
}
else{
if(numZeroes==n/2 && numOnes<n/2){
*arr[row][col]=1;
}
if(numZeroes<n/2 && numOnes==n/2){
*arr[row][col]=0;
}
}
}
}
for(int row=0;row<n;row++){
for(int col=0;col<n;col++){
printf("%d ",*arr[row][col]);
}
printf("\n");
}
return 0;
}
The function should be declared like
void init(int n, int arr[][n]);
(the return type int of the function does not make a great sense.)
and called like
init(dim, matrix);
Within the function instead of statements like this
*arr[row][col]=zero_or_one;
you have to write
arr[row][col]=zero_or_one;
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 );
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.
I have a very simple question.
in this piece of code when will the value of n be decremented?
#include<stdio.h>
void func(int n)
{
//text//
}
int main()
{
int n=10;
func(n--);
return 0;
}
now when func() is called is the value of n decremented when control comes back to main() or is it decremented at that time only but n=10 is passed to func().
Please explain, also if there is a way to check the value then that will be really helpful.
When a function is called, all it's arguments are evaluated (in an implementation-defined order) before the function can start - it's a sequence point. So, after all the arguments are evaluated the function can finally begin.
What this means is that n-- is evaluated and yields the value 10 for the function. At the moment the function has begun n is already 9 but the n parameter of the function hold the value 10.
A simple way to check this:
void func(int n, int *np)
{
printf("Outside: %d\n", *np);
}
int main(void)
{
/* ... */
func(n--, &n);
}
The decrement will happen before the call to func, however func will be passed a copy of the old value still.
Consider the following modification to your program which illustrates this:
#include <stdio.h>
static int n;
void func(int m)
{
printf("%d,%d\n", n, m);
}
int main()
{
n = 10;
func(n--);
return 0;
}
Prints:
9,10
I think your question is better expressed by this code:
#include <stdio.h>
static int global_n;
void func(int n)
{
printf("n = %d, global_n = %d\n",
n, global_n);
}
int main()
{
global_n = 10;
func(global_n--);
return 0;
}
This demonstrates that the function is passed the old value, but the decrement happens before the call.
n = 10, global_n = 9
I'm writing a simple test program to pass multidimensional arrays. I've been struggling to get the signature of the callee function.
The code I have:
void p(int (*s)[100], int n) { ... }
...
{
int s1[10][100], s2[10][1000];
p(s1, 100);
}
This code appears to work, but is not what I intended. I want the function p to be oblivious whether the range of values is either 100 or 1000, but should know there are 10 pointers (by use of function signature).
As a first attempt:
void p(int (*s)[10], int n) // n = # elements in the range of the array
and as a second:
void p(int **s, int n) // n = # of elements in the range of the array
But to no avail can I seem to get these to work correctly. I don't want to hardcode the 100 or 1000 in the signature, but instead pass it in, keeping in mind there will always be 10 arrays.
Obviously, I want to avoid having to declare the function:
void p(int *s1, int *s2, int *s3, ..., int *s10, int n)
FYI, I'm looking at the answers to a similar question but still confused.
You need to transpose your arrays for this to work. Declare
int s1[100][10];
int s2[1000][10];
Now, you can pass these to a function like this:
void foo(int (*s)[10], int n) {
/* various declarations */
for (i = 0; i < n; i++)
for (j = 0; j < 10; j++)
s[i][j] += 1
}
Because of the way the C type system works, an array argument can only be "flexible" in the sense you want in its left-most index.
You could also create a struct for the matrix and pass it to the function p
struct Matrix{
int **array;
int n;
int m;
};
void p(Matrix *k){
length=k->m;
width=k->n;
firstElement=k->array[0][0];
}