I'm trying to write a program which includes an array that filled by user and find a value in it which specified by user then print if it found and count of that number in array.But it works only for first element of array.My code is below:
`void searchh(int arr[],int search,int number,int counter);
int main()
{
int number,search,i;
int counter=0;
printf("How many numbers will you enter?");
scanf("%d",&number);
int array[number];
for(i=0;i<number;i++){
printf("Please enter the %d. element of the array:",i+1);
scanf("%d",&array[i]);
}
printf("Please enter the number that you're looking for:");
scanf("%d",&search);
searchh(array,search,number,counter);
return 0;
}
void searchh(int arr[],int search,int number,int counter){
int i,c;
int key=search;
int num=number;
counter=0;
int arrsize=(int)(sizeof(arr)/sizeof(int));
int arrayent[(int)(sizeof(num)/sizeof(int))];
for(i=0;i<arrsize;i++)
{
if(arr[i]==key)
{
arrayent[counter]=i;
counter++;
}
}
printf("The number that you're looking for which is %d is found %d times.\nLocations:",key,counter);
if(counter>0){
for(c=0;c<sizeof(arrayent)/sizeof(int);c++){
printf("%d\n",arrayent[c]);
}
}
else
printf("Number doesn't exist!!");
}`
And Outputs:
Thanks for your helps.
int arrsize=(int)(sizeof(arr)/sizeof(int));
This already doesn't do what you think it does. sizeof(arr) - could be 4 if size of pointer is 4 bytes. In other words you can't check array size like that inside function, arr decays to pointer of first element of array. Hence sizeof(arr) will return size of pointer which could be 4 or 8. You need to pass the number of elements of the array to the function as parameter - which is number in your case.
This:
int arrayent[(int)(sizeof(num)/sizeof(int))];
is also strange. num is int. sizeof(num) and sizeof(int) will be same - and division will give you 1.
IMO these two lines
int arrsize=(int)(sizeof(arr)/sizeof(int));
int arrayent[(int)(sizeof(num)/sizeof(int))];
should just go as
int arrsize = number;
int arrayent[number];
PS. Also try to use a debugger to help you with some kind of issues.
Related
I'm trying to create an array which has size depended upon input elements count. After that I want to print it but I'm getting very strange outputs.
int main(void)
{
int input_arr;
int i,size=0;
int arr[size];
while(input_arr!=-1){
printf("enter positive int");
scanf("%d",&input_arr);
arr[size]=input_arr;
printf("%d",arr[size]);
for(i=0;arr[i]!='\0';i++){
printf("%d ",arr[i]);
}
size+=1;
}
return 0;
}
33 3 3 3 3 3 6487488 enter positive int3.
It gives output like this and after a while it stops taking elements. I could not recognize where am I doing wrong.
In C the size of the array is fixed the moment you define it. Increasing the size variable does not increase the array size. Therefore you immediately get a buffer overflow the moment you read the first element. You can instead declare a large array like this:
static const int maxSize = 4096;
int arr[maxSize];
int main(void)
{
int i, size=0;
while(size < maxSize){
printf("enter positive int");
scanf("%d", &arr[size]);
++size;
for(i=0; i < size; i++){
printf("%d ",arr[i]);
}
printf("\n");
}
return 0;
}
Alternatively you can use malloc and realloc to grow the array dynamically.
I've written a function to rotate an array,
I've checked and it works when code is in one block I.E. everything
is coded under main(), but when I divide the code so that the rotation is done under a different function I can't get it to work (it truncates instead of rotating).
I'm pretty sure it's something to do with the array pointer.
sorry complete noob
please help:
#include<stdio.h>
void rotate(int *arr,int length);
int main()
{
// this code creates an array via input
int length;
int i;
int num;
printf("enter length of array\n");
scanf("%d",&length);
int arr[length];
for (i=0;i<length;i++) {
printf("enter number\n");
scanf("%d",&num);
arr[i]=num;
}
// just prints original
for(i=0;i<length;i++){
printf("original arr[%d]=%d\n",i,arr[i]);
}
//runs rotate function
rotate(arr,length);
return 0;
}
//the rotate function inputs rotation amount and uses nested for loop to
execute
void rotate(int *arr,int length)
{
int n;
printf("by how many do you want to rotate array?");
scanf("%d",&n);
int i;
int j;
int temp;
for (j=0;j<n;j++)
{
temp=arr[0];
for (i=0;i<length-1;i++)
{
arr[i]=arr[i+1];
}
arr[length-1]=temp;
printf("rotated arr[%d] = %d\n",i,arr[i]);
}
}
my output looks like this:
enter length of array
5
enter number
1
enter number
2
enter number
3
enter number
4
enter number
5
original arr[0]=1
original arr[1]=2
original arr[2]=3
original arr[3]=4
original arr[4]=5
by how many do you want to rotate array?
3
rotated arr[4] = 1
rotated arr[4] = 2
rotated arr[4] = 3
RUN FINISHED; exit value 0; real time: 9s; user: 0ms; system: 0ms
In C you need to declare the function before "main" function, or do the declaration and definition both above the main function. Also do share your error message, for help.
Also, in C language you can't really create dynamic arrays like that( i.e. taking an integer value and then defining the size of array using it "int array[integer] " is wrong of doing it, if the value of integer is being given during runtime)
Read http://www.mathcs.emory.edu/~cheung/Courses/255/Syllabus/2-C-adv-data/dyn-array.html , or any other tutorial about dynamic arrays in C and how to use malloc and calloc.
According to me the problem is in your last print statement in rotate method which is inside the loop.
you must loop through the whole array again to print the rotated array.
like this.
void rotate(int *arr,int length)
{
int n;
printf("by how many do you want to rotate array?");
scanf("%d",&n);
int i;
int j;
int temp;
for (j=0;j<n;j++)
{
temp=arr[0];
for (i=0;i<length-1;i++)
{
arr[i]=arr[i+1];
}
arr[length-1]=temp;
}
for(i=0;i<length;i++){
printf("original arr[%d]=%d\n",i,arr[i]);
}
}
this will work.
And also, Always define functions at the top in c.
how do i correct this
i didn't use structure intentionally
this is a program to input student's name, subject and marks.
in the last block, the array (subject+f) 's 1st subscript is returning garbage values while the rest subscript are returning desired result.
i have also posted the image of output as link.
#include<stdio.h>
#include<string.h>
int main()
{
int size,i,k,sub,a=0,reference;
int temp,sorted;
char temp_s[10];
char temp_sb[10];
printf("enter the size of class\n");
scanf("%d",&size);
printf("how many subjects are there?\n");
scanf("%d",&sub);
reference = sub;
char name[size][20];
char subject[size*sub][20];
int marks[sub*size];
int total,subtotal,retotal;
for(k=0;k<sub;k++)
{
printf("so what's the no. %d subject\n",k+1);
scanf(" %s",(subject[k]));
}
for(i=0;i<size;i++)
{
int j,k=0;
printf("Enter a name of student %d\n",i+1);
scanf(" %s",(name+i));
for(j=a;j<reference;j++)
{
printf("enter marks of %s\n",(subject[k]));
scanf("%d",(marks+j));
k++;
}
a=j;
reference=sub+j;
}
reference=sub;
a=0;
printf("\n list of students and marks:\n");
for(i=0;i<size;i++)
{
int j,f=0;
printf("%s\n",(name+i));
for(j=a;j<reference;j++)
{
printf("%s %d\n",(subject[f]),(marks[j]));
f++;
}
a=j;
reference=sub+j;
}
}
Besides the problem with length of names and subjects, this here is a major problem:
(subject+k)
You are probably misunderstanding the subject[k] and *(subject + k) equivalent.
The variable subject is an array of arrays. That means subject[i] is an array (of char and can be used as a zero-terminated string).
The expression (subject + k) is a pointer to the array in subject[k]. It's equal to &subject[k] which have the type char (*)[10]. It's can not be used as a zero-terminated string without dereferencing. So either use *(subject + k) or the simple, less-to-write and easier-to-read subject[k].
I think you also need to change
int marks[sub];
to
int marks[size * sub];
one mark for each subject for each student, correct?
problem:
Write a C program to implement Binary Search Algorithm.
Include a function
int BinarySearch (int, int, int *, int x) --- The 1st parameter is the lower limit of the list or array, the 2nd parameter is the upper limit of the list or array, the third parameter is a pointer to the array and the fourth parameter is the search element.
Please note that the index of the search element is returned by the function. If the search element is not present in the array, -1 is returned.
Assume that the maximum size of the array is 10 . Please note that if a is the array, then a[0] is in position 0, a[1] is in position 1 ...
I tried this ,but it's test cases passes says only 75%,and it can't find the element if n=1;Thanks in advance :)
#include<stdio.h>
int BinarySearch(int, int ,int *, int);
int main(){
int first=0, last;
int a[20],search,s=0,n,i;
//int j,temp=0;
printf("Enter the number of elements :\n");
scanf("%d",&n);
printf("Enter the elements :\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be searched :\n");
scanf("%d",&search);
last=n-1;
s=BinarySearch(first, last, a, search);
if(s>0){
printf("The element %d is in position %d",search,s);
}
else{
printf("The element %d is not present in the array",search);
}
return 0;
}
int BinarySearch(int l, int h, int *a, int x)
{
int mid;
while(l<=h){
mid=(l+h)/2;
if (x==a[mid])
return mid;
else if(x<a[mid])
h=mid-1;
else if(x>a[mid])
l=mid+1;
}
return -1;
}
I have the following source and when I execute it the values are getting their signs changed. I am not able to find out where I am going wrong. Any suggestions is helpful
Code
#include <stdio.h>
#include <stdlib.h>
int arrsort(int *arr, int size);
int display(int *arr, int size);
int main()
{
int s_o_1=0, s_o_2=0;
int i; //index for arrays
int a[s_o_1],b[s_o_2];
printf("enter the size of the first array\n");
scanf("%d",&s_o_1);
printf("Enter the values of the first array\n");
for (i=0;i<s_o_1;i++)
{
scanf("%d",&a[i]);
}
printf("enter the size of the second array\n");
scanf("%d",&s_o_2);
printf("Enter the values of the second array\n");
for (i=0;i<s_o_2;i++)
{
scanf("%d",&b[i]);
}
//sort the first array
arrsort(a,s_o_1);
printf("The sorted first array is\n");
display(a,s_o_1);
//sort the second array
arrsort(b,s_o_2);
printf("The sorted second array is\n");
display(b,s_o_2);
}
int arrsort(int *arr, int size)
{
int temp; //for holding the temp value
int i; //for indexing
int j;
for(j=0;j<size;j++)
{
for(i=0;i<size;i++)
{
if(arr[i]>arr[i+1])
{
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
}
}
}
int display(int *arr, int size)
{
int i; //array index
for (i=0;i<size;i++)
{
printf("%d\t",i);
}
printf("\n");
for (i=0;i<size;i++)
{
printf("%d\t",arr[i]);
}
printf("\n");
}
Output
enter the size of the first array
5
Enter the values of the first array
1 5 -10 -15 3
enter the size of the second array
5
Enter the values of the second array
-3 -5 15 9 10
The sorted first array is
0 1 2 3 4
-15 -10 3 5 10
The sorted second array is
0 1 2 3 4
-15 -10 -5 -3 9
The problem is the array declaration:
int s_o_1=0, s_o_2=0;
int i; //index for arrays
int a[s_o_1],b[s_o_2];
The arrays are probably declared with size 0. Either declare with an appropriate maximum size, or declare after reading the sizes for the arrays.
Your code has undefined behaviour. In this line:
int a[s_o_1],b[s_o_2];
It declares arrays with zero size. When later get values for s_o_1 and s_o_2 the array size won't change. So all your reads & writes lead to undefined behaviour.
C standard requires array should be of non-zero length.
The way you reserve memory is not correct int a[s_o_1],b[s_o_2];
You must use int *a, *b; and later after scanf("%d",&s_o_1); you need to do something like a = (int*)malloc(sizeof(int)*s_o_1);
The same goes to allocating memory for b.
Also bubble sort alghorithm should be something like bellow
for(j=0;j<size - 1;j++)
{
for(i=j + 1;i<size;i++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}