Pointers to min and max values of a array [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm trying to write a program in which there are two pointers that point to the min and the max value of a 100-values array. Why does it give me error?
#include <stdio.h>
int main(void){
int array[100], i;
for(i=0; i<100; i++)
array[i]=i;
int *ptr1, *ptr2, flag=0;
ptr1 = &array[0];
ptr2 = &array[0];
while(!flag){
for(i=0;i<100;i++){
if(*ptr1 > array[i]){
ptr1 = &array(i);
break;
}else if(*ptr2 < array[i]){
ptr2 = &array(i);
break;
}
}
if(i==100)
flag=1;
}
printf("%d %d", *ptr1, *ptr2);
}
main.c:13:25: error: called object ‘array’ is not a function or function pointer

The statement
ptr1 = &array(i);
should be corrected to
ptr1 = &array[i];
So does the
ptr2 = &array(i);

Related

How to determine the number of appearances of a letter in a string using C language? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
This is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int appar(char c[], char x);
int main() {
char c[] = "hello everyone!";
int b = appar(c, 'h');
printf("nbr of h is %d ", b);
return 0;
}
int appar(char c[], char x) {
int i = 0, cmpt = 0;
int q = strlen(c);
for (i; i < q; i++) {
if (c[i] == 'x')
cmpt++;
}
return cmpt;
}
I run and compile the program, but I receive "nbr of h is 0".
What's the wrong in this code?
Change c[i]=='x' to c[i]==x
You want to compare with the variable x, not the character constant 'x'

Static Variables not retaining their values inside function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Inside functions static variables retain their value when called again but why it is not happening in this case ?
#include <stdio.h>
void print(void) {
static int x;
x = 10;
x += 5;
printf("%d ", x);
}
int main() {
print();
print();
return 0;
}
It is giving output as 15 15.
You assign a new value each time:
static int x;
x = 10; // <- assignment
What you probably want is initialization:
static int x = 10; // <- initialization
Initialization happens only once during variable lifetime, and static means the variable lives for the whole execution time of your program.
Initializing static variable will solve the issue. Below corrected example resolves the issue and gives me 15 and 20
#include <stdio.h>
void print(void) {
static int x = 10; //Initialize here
// x = 10; //remove this statement
x += 5;
printf("%d ", x);
}
int main() {
print();
print();
return 0;
}

C Arrays gives address instead of value when second array is created [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
First of all, sorry for my English. I hope you will still understand what my problem is.
I am new to C programming and I am a bit confused. Here is my code.
#include <stdio.h>
#include "tableaux.h"
int main(int argc, const char * argv[]) {
int tableauUn[4] = {1, 1, 1, 1};
printf("%d\n", sommeTableau(tableauUn, 4));
printf("%d\n", moyenneTableau(tableauUn, 4));
return 0;
}
And this is the file where my functions are. I also have a file where I have my prototypes.
#include <stdio.h>
#include "tableaux.h"
int sommeTableau(int tableau[], int taille) {
int resultat;
for (int i = 0; i < taille; i++) {
resultat += tableau[i];
}
return resultat;
}
int moyenneTableau(int tableau[], int taille) {
int resultat;
for (int i = 0; i < taille; i++) {
resultat += tableau[i];
}
return resultat / taille;
}
void copierTableau(int tableau[], int taille, int tableauDeux[]) {
for (int i = 0; i < taille; i++) {
tableauDeux[i] = tableau[i];
}
}
So everything works fine. The first printf gives me the total of the values that are stored in the array and the second one gives me the average of the values.
5
1
Program ended with exit code: 0
What I don't understand is why do I get this result when I want to create a second array ?
int tableauUn[4] = {1, 1, 1, 1};
int tableauDeux[4] = {0};
the result
1606416356
1
Program ended with exit code: 0
So I haven't used the second array but the result of the first printf changes and I am a bit confused with what is going on.
I hope you can help me !
Please initialize the sum you return in function
int resultat = 0;
otherwise resultat will take garbage value as initial value

Swap two elements of an array function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Can you help me by telling me what is wrong with this? Why is the swap function not working?
void swap(int a[], int b, int c) {
int temp = a[b];
a[b] = a[c];
a[b] = temp;
}
void bubble1 (int a[], int N){
int i;
for(i=0;i<N-1;i++){
if(a[i]>a[i+1]){
swap(a,i,i+1);
}
}
}
void main() {
int N = 11;
int a[12]={5,3,12,4,25,10,14,35,2,8,13};
bubble1 (a,N);
int i;
for(i = 0; i < N; i++){
printf("%d\n",a[i]);
}
}
If I don't use the swap function and do the swapping manually in the "bubble" function it works. However if I use the swap it doesn't work, even though it's exactly the same. What am I doing wrong here?
int temp = a[b];
a[b] = a[c];
a[b] = temp;
Simple typo, you are assigning to a[b] twice. The second one should be a[c]

What is output of following program? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
int main()
{
int i = 10;
int *p = &i;
foo(&p);
printf ("%d ", *p);
printf ("%d ", *p);
}
void foo(int **const p)
{
int j = 11;
*p = &j;
//Printing the vlue
printf("%d ", **p);
}
When foo returns, the pointer p in main points to a local variable that existed during the execution of foo. Since foo has ended, de-referencing that pointer invokes undefined behaviour. Therefore your program can output, or indeed do, anything.

Resources