hello,
i'm a complete beginner in C programming and would love to get your help with somthing. a attached the assignment.
thank you!!
hello,
i'm a complete beginner in C programming and would love to get your help with somthing. a attached the assignment.
thank you!!
It is better for you and for us to first post your code,your actually attempt in your assignment and then ask something based on this.
it's sad, but the only thing i mannaged to do is this:
if (choice==1)
{
printf("Enter matrix size:");
scanf("%d",&matrix_size);
while (matrix_size<1 || matrix_size>10)
{
printf( "**** Incorrect input ****\n");
Not bad. I guess you have declared matrix_size as int.You should write your code like this
[key]<YOUR CODE GOES HERE>[/key].Replace word key with in order this to work.
So the problem you have is to how read a row of the array.You should read every line seperately,probably with fgets(search the web for reference and you find something like this fgets - C++ Reference).
Or you should check something like this How to read a line from the console in C - Stack Overflow (found it by searching for 'how to read a line in c'.
However i am hesitant to say more because the hw says that you should only use what you are taught in class,but we aren't in the same class :/
oh sorry. the things i learned: if, else if, while, do while, getchar, putchar, printf, scanf, for, %, >,<,==,&&,||,!,define, arrays,gets,puts.
I can not allow to myself to suggest you using gets.(possible overflow of your buffer,no good function)
You could use getchar to read char by char the input and handle these with the ASCII codes.Of course no need to know them by heart.
e.g.
Of course this is not very convenient,but using gets is a bad concept.Code:#include <stdio.h> int main(void) { int c; c=getchar(); if(c=='5') { printf("ASCII code of input : %d\n",c); printf("Value of input : %c\n",c); } else printf("Nothing happens\n"); return 0; }
thank you so much. but i can't use return.. and we usely write void main, and not int main..does in matter ? what is buffer overflow ?
somthing i didn't understand.. why c==5 ? why 5 specifically?
can you explain what exactlly you did in hear ? this is after i write the array ? i do it using nested for ?
Yes! Using void main() is wrong. Every version of the C standard insists that main return an int.thank you so much. but i can't use return.. and we usely write void main, and not int main..does in matter ?
What? The return statement is a required element of every program, according to the C standards main should return an int.but i can't use return..
A buffer overflow is when you attempt to read past the end of an array. And since gets() doesn't enforce any size limitations it is particularity susceptible to this problem.what is buffer overflow ?
Jim
and if i wanted to use gets anyway? how would it would look like ?
and alos, how do i make sure that there are only numbers in there and not letters and there not 0 ? and that the amout of numbers that the user put is the amout that they asked in the exercise?
really we always use void main and no return.. at least for now.
I used 5 for instance.Same logic will make you read 1,2,3 etc. .What i did there is to write a single character from the user.Then assign this value to variable c which is declared as a char.Then i just print the values.Mind that with %d,i print the ASCII code of 5,which is 53(no need to know these by heart) and with %c i printed the value of 5,which is five.
As for the int main(void) and return 0 it is ok for some compilers.They do this by default.I remember that during the first weeks in DIT,we also used it the same way.No need to focus there now.Focus on your hw!
I you want to insist on learning something that is so dangerous, I suggest you try to find out yourself. I won't help you learn something that is so bad that the C standard has removed this function from the language.and if i wanted to use gets anyway? how would it would look like ?
You need to check each character to insure it is a digit.how do i make sure that there are only numbers in there and not letters and there not 0 ?
Then you are learning how not to write proper C programs, I suggest you ask your instructor why they are teaching you wrong!really we always use void main and no return.. at least for now.
Jim
And in order to check if a number is not 0 you just check with
[code]if(number>0)
/*then we are ok*/
else
/*not ok*/[/code
@Jim i am sure it is because they are at the start of it.In the near future the instructor will taught about how main is supposed to be written.
As for the gets,it is not just me and jim that refuse(for your own good ) to show you how it works,but if you search the web many other people suggest never use gets!
SAMARAS is right.. we are just at the start.
how do i cheak each charecter ti insure it is a digit ?
The right thing is that you post your attempt as i said before.
Hint:Check if the character is a number by using operators >,<,=,>=,<= to ensure that the character is at the range of a number.If it is not,then it not a number.Use it a loop in order to check all the characters.
e.g.
[code]if( character<='0' || character>='9')
/*then you should figure out what must be done*/
[/code