Hello there!
I'm kind of confused , I literally tried every possible sollution i could think of.
Yet the program isn't functioning right ! The purpose of this program is to seperate the odd numbers and the pair numbers from one single array and put each one of them on an array!
When i execute , and after putting some simple numbers , another random big numbers appear in each array!
why is that and how can i fix this ?
void Pairs_impairs(int* n,int T[], int P[], int Imp[]){
int i,j=0;
for (i=0;i<*n;i++){
if(T[i]%2==0){
P[j]=T[i];
j++;}
else{
Imp[j]=T[i];
j++;
}}
*n=j;
}
int main(){
int t[100],p[100],imp[100];
int n;
puts("saisir n :");
scanf("%d",&n);
puts("saisir le tableau : ");
int i;
for(i=0;i<n;i++){
scanf("%d",&t[i]);
}
for(i=0;i<n;i++){
printf("%d ",t[i]);
}
Pairs_impairs(&n,t,p,imp);
printf("\nLes pairs : ");
for(i=0;i<n;i++){
printf("%d ",p[i]);
}
printf("\nLes impairs : ");
for(i=0;i<n;i++){
printf("%d ",imp[i]);
}
return 0;
}
The function is incorrect. You need to keep separate indices for the arrays P and Imp within the function. For example
void Pairs_impairs(int* n,int T[], int P[], int Imp[]){
int i,j1=0, j2 = 0;
for (i=0;i<*n;i++){
if(T[i]%2==0){
P[j1]=T[i];
j1++;}
else{
Imp[j2]=T[i];
j2++;
}}
*n=j1;
}
Also you should guarantee that the both arrays P and Imp will have equal number of elements. Otherwise you need to return from the function two indices.
For example
struct Pair
{
size_t first;
size_t second;
};
struct Pair Pairs_impairs( const int T[], size_t n, int P[], int Imp[] )
{
struct Pair p = { .first = 0, .second = 0 };
for ( size_t i = 0; i < n; i++ )
{
if ( T[i] % 2 == 0 )
{
P[first++] = T[i];
}
else
{
Imp[second++] = T[i];
}
}
return p;
}
Related
This is a piece of code to add the same number multiple times to an empty array but when I am printing the now non empty array, I am getting some other values:
#include<stdio.h>
#include<stdlib.h>
void sort_0(int arr[100], int i, int n){
int final_array[100], c=0;
// Count the number of '0' in the array
for(i=0;i<n;i++){
if(arr[i] == 0){
c++;
}
}
// Add the c number of '0' to the final_array
for(i=0;i<c;i++){
scanf("%d",final_array[i]);
}
for(i=0;i<c;i++){
printf("%d ", final_array[i]);
}
}
int main(){
int arr[100], i, n;
// Entering the size of the array
scanf("%d", &n);
// Entering n elements into the array
for(i=0;i<n;i++){
scanf("%d", &arr[i]);
}
sort_0(arr,i,n);
return 0;
}
In the above code, the number of times 0 appears in the array is counted. Then the count is taken as the range and 0 is adding to the empty array final_array count times.
If c = 5, the final_array = {0,0,0,0,0}
Expected Output:
arr = {0,1,4,3,0}
Output = 2
I am not getting any output
Since you don't know how much 0 you'll need to add to your array_final I figured out that a better solution could be to create that array after you have the number of 0 of the first array. Also, I see no reason why you were passsing i to the function since you can simply define it in the function itself.
void sort_0(int arr[10], int n, int* c){
int i;
for(i=0;i<n;i++){
if(arr[i] == 0){
(*c)+= 1;
}
}
}
int main (void) {
int size;
printf("Enter array size: ");
scanf("%d", &size);
int arr[size];
for (int i=0;i<size;i++) {
scanf("%d",&arr[i]);
}
int c = 0;
sort_0(arr, size, &c);
printf("C is: %d\n",c);
int* final_array;
if ((final_array=malloc(c * sizeof(int)))==NULL) // should always check malloc errors
{
perror("malloc");
return -1;
}
for (int i=0;i<c;i++) {
final_array[i]= 0;
}
printf("{");
for (int i=0;i<c-1;i++) {
printf("%d,", final_array[i]);
}
printf("%d}\n",final_array[c-1]);
return 0;
}
In below code, in main function if I remove "p = createHeap(n,a)", then its taking input normally till defined n. But if its not removed then array input (for loop) will go on taking input continuously (infinite loop). Not able to get the reason.
#include<stdio.h>
int * createHeap(int n, int *b);
void insert();
void delete();
int main() {
int n,i,j;
int *p;
printf("Enter the size of array : ");
scanf("%d",&n);
int a[n];
printf("\nEnter the elements of array: %d",n);
for(i=0;i<n;i++) {
scanf("%d",&a[i]);
}
printf("out");
p = createHeap(n,a); // Problem in this line.
//If removed then array will take input upto n
// if not then input loop will be infinite.
printf("\nsuccess");
return 0;
}
int * createHeap(int n, int *b) {
int i,j,k;
for(i=1;i<n;i++) {
j=(i+1)/2 - 1; //parent node
k=i;
while(j>=0) {
if(b[k] > b[j])
{
b[i] = b[i]+b[j]; //swap
b[j] = b[i]-b[j];
b[i] = b[i]-b[j];
}
k=j;
j=(i+1)/2 - 1; //tracking parent node
}
}
return b;
}
I am writing a program to generate all possible permutations of a given series of numbers and then generate all possible binary trees from that permutations so, what I thought is having a program which generates permutations and stores the result to a file and then write further code to read line by line (which has all permutations ) and generate binary trees out of them, so right now I have written half program which generates permutation and it stores the result in file.
#include <stdio.h>
//function to print the array
void printarray(int arr[], int size)
{
FILE *fp;
int i,j;
fp=fopen("result.txt","w");
for(i=0; i<size; i++)
{
// printf("%d\t",arr[i]);
fprintf(fp,"%d\t",arr[i]);
}
printf("\n");
fclose(fp);
}
//function to swap the variables
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
//permutation function
void permutation(int *arr, int start, int end)
{
if(start==end)
{
printarray(arr, end+1);
return;
}
int i;
for(i=start;i<=end;i++)
{
//swapping numbers
swap((arr+i), (arr+start));
//fixing one first digit
//and calling permutation on
//the rest of the digits
permutation(arr, start+1, end);
swap((arr+i), (arr+start));
}
}
int main()
{
//taking input to the array
int size;
printf("Enter the size of array\n");
scanf("%d",&size);
int i;
int arr[size];
for(i=0;i<size;i++)
scanf("%d",&arr[i]);
//calling permutation function
permutation(arr, 0, size-1);
return 0;
}
but the problem here in this program is that this program only stores one permutation and does not stores other permutations in result.txt file, how do I go on storing result this way. Also program does not ends a blank cursor blinking which gives a false impression of infinite while loop.
I had to press Ctrl+c to end the program how to get rid of this?
your fopen("result.txt","w"); truncates file each time opened.
use fopen("result.txt","a"); instead
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#define N 10
void print(int *num, int n)
{
FILE *fp;
fp=fopen("result.txt","a");
int i;
for ( i = 0 ; i < n ; i++)
// printf("%d ", num[i]);
fprintf(fp,"%d ",num[i]);
fprintf(fp,"\n");
fclose(fp);
}
int main()
{
int num[N];
int *ptr;
int temp;
int i, n, j;
printf("\nHow many number you want to enter: ");
scanf("%d", &n);
printf("\nEnter a list of numbers to see all combinations:\n");
for (i = 0 ; i < n; i++)
scanf("%d", &num[i]);
for (j = 1; j <= n; j++) {
for (i = 0; i < n-1; i++) {
temp = num[i];
num[i] = num[i+1];
num[i+1] = temp;
print(num, n);
}
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
int crp(int mtrs[],int size)
{
int a=1;
int i;
for(i=0;i<size;++i)
{
a*=mtrs[i];
}
return a;
}
int main()
{
int k,size;
int **mtrs;
printf("enter the size of the matrix:");
scanf("%d",&size);
mtrs=(int**)malloc(size*sizeof(int*));
if( mtrs == NULL )
printf( "Yetersiz bellek!" );
printf("enter the input numbers of matrix:");
for(k=0;k<size;k++)
{
mtrs[k] =(int*) malloc( size* sizeof(int) );
if( mtrs[k] == NULL )
printf( "not enough memory!" );
}
for(k=0;k<size;k++)
{
scanf("%d",&mtrs[k]);
}
printf("\n\n");
for(k=0;k<size;k++){
printf("%d ",mtrs[k]);
}
printf("\n\n");
printf("Elemanlar carpimi %d dir.",crp(mtrs[k],size));
return 0;
}
I get the problem of access violation reading location 0xFDFDFD.
it stucks at the location of a*=mtrs[i];.. The program's aim is to multiplication of the entered numbers
I am not sure if I am using the malloc in a correct way.
This code snippet is invalid
for(k=0;k<size;k++)
{
scanf("%d",&mtrs[k]);
&&&&&&&&
}
You should write for example
int m;
//...
for(k=0;k<size;k++)
{
for ( m = 0; m < size; m++ )
{
scanf("%d", &mtrs[k][m]);
}
}
The same is valid for this loop
for(k=0;k<size;k++){
printf("%d ",mtrs[k]);
}
It should look like
for(k=0;k<size;k++)
{
for ( m = 0; m < size; m++ )
{
printf("%d ",mtrs[k][m]);
}
printf( "\n" );
}
The function should be defined like
long long int crp( int * mtrs[],int size)
^^^^^^^^^^^^^ ^^^^^^^^^^^^
{
long long int a=1;
int i, j;
^^^^^^^^^
for(i=0;i<size;++i)
{
for ( j = 0; j < size; j++ )
{
a*=mtrs[i];
}
}
return a;
}
And the result of the function should be outputted like
printf("Elemanlar carpimi %lld dir.",crp(mtrs,size));
^^^^^
I do not fully understand what is your code point, but you have various mistakes and logic problems with it.
printf("%d ",mtrs[k]); and scanf("%d",&mtrs[k]); expect int but you passed *int (aka int[]), you probably need to insert/read values using 2 nested for loops.
printf("Elemanlar carpimi %d dir.",crp(mtrs[k],size)); last cycle is for(k=0;k<size;k++), according to it k = size, so mtrs[k] is out of bounds, which cause application crash.
I am trying to run this code on Dev C++ but it keeps on crashing after the user inputs the two numbers. The program takes input m and n from user two numbers and then returns the output as the solution of the function A which is:
A(m,n) = A(m,n-1)+ A(m-1, n) , if m,n >0
A(m,n) = m-n if m or n <0
Can anybody please tell me why is it happening?
#include<stdio.h>
#include<stdlib.h>
int main() {
int num1=0;
int num2=0;
int rows=0;
int columns=0;
int i,j,**array;
printf("Enter two non-negative integer numbers \n");
scanf("%d %d",&num1,&num2);
//create 2d-Array
rows=num1+1;
columns=num2+1;
array=malloc(rows * sizeof(int *));
for(i=0;i<rows;i++)
{
array[i]=malloc(columns*sizeof(int));
}
//Fill data in array
computeArray(array,rows,columns);
// Display contents of array
for (i = 0; i < rows; i++ )
{
for(j= 0; j < columns; j++ )
{
printf("array[%d][%d] = %d\n", i,j, array[i][j] );
}
}
getch();
return 0;
}
int computeArray (int **array, int rows, int columns) {
int i,j;
for(i=0; i<rows;i++)
{
for(j=0;j<columns;j++)
{
array[i][j]=computeFunction(array,i,j);
}
}
return **array;
}
int computeFunction(int **array, int i, int j) {
int value=0;
if((i<0)||(j <0))
{
value = i-j;
printf("%d",value);
return value;
}
else
{
value = (array[i][j-1] + array[i-1][j]);
printf("%d",value);
return value;
}
return value;
}
When program's behavior is undefined, anything could happen. You should declare a prototype for the function computeArray and computeFunction before main:
int computeArray (int **array, int rows, int columns);
int computeFunction(int **array, int i, int j);
and change
if((i<0)||(j <0)) {...}
in computeFunction to
if((i<=0) || (j <= 0)){...}
&& instead of || may help.
The code fails at the
value = (array[i][j-1] + array[i-1][j]);
line, when j==0.
Debuggers tend to be very useful for spotting simple mistakes. Use them.